Has Yii auto-typecasting for database fields?


I’ve read the forums half of my day but I can’t realize why Yii converts all of my active record, dao, etc. database datatype to string when I read one from a model with getter (eg. $post->status or $post->getStatus()). I think Yii has to typecast all of my data according to the mysql schema.

For example I have a status field defined in post model as a tinyint mysql datatype (also tried unsigned tinyint), the Gii considered it as an integer and generated the proper phpdoc - but when I call this property ($post->status) the datatype will be always string. I’ve read in the forums that there is a problem with integer typecasting, but in my case I’ll be satisfied with tinyint detection.

The strange thing is that I checked the CMysqlColumnSchema extractType function for this field and the Yii is properly considered my status column as integer, but in the AR and DAO object the type of status property is always inappropriate. I’ve also checked blog demo and I found that the integer fields are all strings as an AR property.

I’m new to Yii so probably I’ve missed something, but I’m from Kohana and I’ve used to auto-typecasting. It’s very embarrassing typecasting individually all of my tinyint status flags to integer.



XAMPP 1.7.7

PHP Version 5.3.8

MySQL 5.5.16

So the question is: has Yii auto-typecasting for database fields?

Thanks for your answers in advance,


No. The PDO driver does return the strings. If you use the native mysql PDO driver (mysqlnd), you could do the following to get the native data types:

'db' => array(


   'emulatePrepare' => false,


However, it’s not safe to rely on that. A simple example: If there’s a large number coming from the database (i.e. larger than PHP_INT_MAX), then it has to be casted into a string anyway. There are probably more problems.

Just get used to it I guess.