Yii2 - ActiveRecord - type casting

2019-09-02 10:47发布

in yii2 activerecord float,double,decimal datatype of mysql not support ! (Or I'm wrong) when i access to this column(float,double,decimal) , yii2 return string.

how i can type casting float,double,decimal datatype of mysql to php float ?

mytable(id int, f1 float, f2 double, f3 decimal(10,2))

for example in activerecord class :

public function filters(){
   // return [['f1','f2','f3'] => 'float', ...];
}

2条回答
走好不送
2楼-- · 2019-09-02 11:01

I know nothing about Yii internals but I can answer for mysql and PDO.

First of all, for the decimal type you cannot get anything but string, as it's the very point of this data type.

For the rest, to make PDO return int and double values with corresponding types from mysql, you have to make sure that both mysqlnd-based driver is installed and emulation mode is turned off. Given that, PDO will return you proper data types from a prepared statement.

查看更多
淡お忘
3楼-- · 2019-09-02 11:11

Yii returns string for unsigned integers and floating numbers. That's because typically DBMS type can represent more than PHP and we want to preserve precision.

查看更多
登录 后发表回答