【发布时间】:2018-05-06 13:43:30
【问题描述】:
我需要将我实体的一个字段保存为数据库中的整数,但在应用程序中它应该被视为浮点数。
目前,我尝试使用 beforeSave、beforeMarshal 等方法。 在数据库中,该字段被标记为整数(11)。在我放置的实体中
* @property float $price
但是,我的实体仍然将此字段视为整数...
在下面的代码中(我把它放在 Table 类中),只有一个地方 var_dump 函数返回 float - 这是因为转换为这种类型。其他地方都是整数。
public function beforeSave(Event $event, EntityInterface $entity, \ArrayObject $options) {
var_dump($entity->get('price'));
die();
$entity->set('price', $entity->get('price') * 100);
return true;
}
public function beforeMarshal(Event $event, \ArrayObject $data, \ArrayObject $options)
{
$data['price'] = (float) $data['price'];
var_dump($data);
}
public function beforeRules(Event $event, EntityInterface $entity, \ArrayObject $options, $operation)
{
var_dump($entity->get('price'));
}
public function afterRules(Event $event, EntityInterface $entity, \ArrayObject $options, $result, $operation)
{
var_dump($entity->get('price'));
}
有谁知道如何更改它,所以我将保存整数,但读取浮点数?
【问题讨论】:
标签: php mysql database cakephp-3.0