DB格式为

时间:2018-07-24 08:35:19

标签: cakephp cakephp-3.0

我想从'last_price'= 9.12345678中写入数据库值(点后需要8个字符)

CakePHP-Last 3.6.7,PHP 7.2x64,MySQL 5.7x64,数据库中的字段“ last_price”类型:DECIMAL 20、8

控制器:

 public function add ()
    {
        // debug ($ this-> request-> getData ());
        $ pair = $ this-> Pairs-> newEntity ();
        if ($ this-> request-> is ('post')) {
            $ pair = $ this-> Pairs-> patchEntity ($ pair, $ this-> request-> getData ());
            // debug ($ pair);
            if ($ this-> Pairs-> save ($ pair)) {
                $ this-> Flash-> success (__ ('The pair has been saved.'));
                return $ this-> redirect (['action' => 'index']);
            }
            $ this-> Flash-> error (__ ('The pair could not be saved. Please, try again.'));
        }
        $ exchanges = $ this-> Pairs-> Exchanges-> find ('list', ['limit' => 200]);
        $ this-> set (compact ('pair', 'exchanges'));
    }

内容$ this->请求-> getData():

[
'exchange_id' => '1',
'title' => '12121',
'last_price' => '9.12345678',
'favorites' => '0'
]

PairsTable.php(模型)

$ validator
            -> decimal ('last_price', 8)
            // -> maxLength ('last_price', 30)
            -> requirePresence ('last_price', 'create')
            -> notEmpty ('last_price');

我单击添加,但是以下查询即将进入数据库:

2018-07-24 07:09:19 Debug: duration = 0 rows = 1 INSERT INTO pairs (exchange_id, title, last_price, favorites, created, modified) VALUES (1, '5453534534543', '9.123457', 0, '2018 -07-24 07:09:19 ',' 2018-07-24 07:09:19 ')

为什么'last_price'= 9.123457?!!! ???? 我看到-它写在数据库中为9.12345700

为什么不是9.12345678?

1 个答案:

答案 0 :(得分:0)

To help you solve this, I set up a table with decimal_value (decimal(18,12)), float_value (float(18,12)), double_value (double(18,12)).

Image of retrieved data from database

double_value was accurate on all three inserts.

Change column type to DOUBLE. Clear the orm_cache.