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

标签 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 错误(如上所述)。

如何解决这个问题?

最佳答案

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)

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

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)

插入正确的值。插入空白时应插入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/

相关文章:

java - 数据截断 : Incorrect datetime value

php - 比较从 MySql 数据库中提取的字符串日期

php - 在 Laravel 上过滤

zend-framework - Doctrine 全外连接

php - hasManyThrough 用于 3 个表

php - 带有大文本选择的 HTML 下拉菜单与页面对齐

php - Laravel 5 imagepng 不工作

php - Zend表单中间有一步,如何修改正常代码

php - 如何在 Zend Form 中添加验证码?

php - PayPal 不断收到无效的 ipn