问题实际上是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错误(如上所述)。

我该如何解决这个问题?

最佳答案

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)

关于php - Zend Framework 2-使用空值的列(float类型)处理空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21550889/

10-10 22:07