Zend Framework 2-使用空值的列(类型float)处理空

时间:2014-02-04 11:13:59

标签: php mysql zend-framework zend-framework2 zend-db

问题实际上是MySQL错误:

Incorrect decimal value: '' for column demand_charge at row 1

我有一个与ZfcUser类似的实体:

class DeviceConfiguration
{
    // ......... other properties
    /**
     * @var float
     *
     */
    private $demandCharge;

    // ......... other methods

    /**
     * Set demandCharge
     *
     * @param float $demandCharge
     * @return DeviceConfiguration
     */
    public function setDemandCharge($demandCharge)
    {
        $this->demandCharge = $demandCharge;

        return $this;
    }

    /**
     * Get demandCharge
     *
     * @return float 
     */
    public function getDemandCharge()
    {
        return $this->demandCharge;
    }
}

我有一个与ZfcUser类似的映射器,其中包含插入数据的方法

class DeviceConfigurationMapper extends AbstractDbMapper
{
    public function insert($device, $tableName = null, HydratorInterface $hydrator = null)
    {
        $device->setLastUpdate(new DateTime());
        $result = parent::insert($device);
        $device->setId($result->getGeneratedValue());
        return $result;
    }        
}

我还有一个类DeviceConfigurationHydrator,它只是Zend\Stdlib\Hydrator\ClassMethods的扩展,用于从实体中提取数据或将数据水合到实体!

字段demand_charge在Mysql数据库中的类型为float,并不是我在InputFilter中定义的必需字段。因此,当用户将字段保持为空时,我会收到SQL错误(如上所述)。

如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

DROP TABLE IF EXISTS testfloat;
Query OK, 0 rows affected (0.11 sec)

CREATE TABLE testfloat(id int,prod_name CHAR(10), price DECIMAL);
Query OK, 0 rows affected (0.29 sec)

您收到消息是因为您在BLANK字段中插入了DECIMAL以下示例

INSERT INTO testfloat(id,prod_name,price) values(1,'A','');
Query OK, 1 row affected, 1 warning (0.00 sec)

show warnings;
+---------+------+---------------------------------------------------------+
| Level   | Code | Message                                                 |
+---------+------+---------------------------------------------------------+
| Warning | 1366 | Incorrect decimal value: '' for column 'price' at row 1 |
+---------+------+---------------------------------------------------------+
1 row in set (0.00 sec)

INSERT INTO testfloat(id,prod_name,price) values(1,'A','ABC');
Query OK, 1 row affected, 1 warning (0.01 sec)

show warnings;
+---------+------+------------------------------------------------------------+
| Level   | Code | Message                                                    |
+---------+------+------------------------------------------------------------+
| Warning | 1366 | Incorrect decimal value: 'ABC' for column 'price' at row 1 |
+---------+------+------------------------------------------------------------+
1 row in set (0.00 sec)

插入正确的值。插入blank

时应插入0.00
INSERT INTO testfloat(id,prod_name,price) values(1,'A',10.00);
Query OK, 1 row affected (0.00 sec)

SELECT * FROM testfloat;
+------+-----------+-------+
| id   | prod_name | price |
+------+-----------+-------+
|    1 | A         |     0 |
|    1 | A         |     0 |
|    1 | A         |    10 |
+------+-----------+-------+
3 rows in set (0.00 sec)

答案 1 :(得分:0)

我到现在为止找到的答案非常简单!我将以下内容添加到DeviceConfigurationHydrator保湿器中。

class DeviceConfigurationHydrator extends ClassMethods
{
    public function extract($object)
    {

        $data = parent::extract($object);
        $field = 'demand_charge';
        if (isset($data[$field]) && empty($data[$field])) {
                unset($data[$field]);
        } 

        return $data;
    }
}

答案 2 :(得分:0)

  

即使它已经很晚了 - 但如果有人会遇到同样的问题

要解决此问题,您可以为demand_charge属性添加以下过滤器。

在Zend Framework 2中:Zend_Filter_Null 在Zend Framework 3中:ToNull

框架内部将空字符串''更改为0,这样您的数据库查询就不会失败。