Thursday, May 24, 2012

cakephp 2 tinyint(1) return as boolean changes

in cake 1.3 always used tinyint(1) for is_active database fields..

echo $this->Form->input('is_active');
this created a checkbox and saves 0 or 1 into the database.

Then i could use this 0/1 very conveniently :

<img src="/img/status_<?php echo $car['Car']['is_active']); ?>.png">

find('all', array('codititons' => array('Car.is_active' => 0)))


With cakephp 2 its no longer returns as 0 :
db : 0 = [is_active] => 1 ; ["is_active"]=> bool(true)
db : 1 = [is_active] => ; ["is_active"]=> bool(false)

Boolean column values will be casted to php native boolean type automatically,  
so make sure you update your test cases and code if you were expecting the 
returned value to be a string or an integer: If you had a "published" column 
in the past using mysql all values returned from a find would be numeric in the past,
now they are strict boolean values.

For now I set :
form helper manually to : echo $this->Form->checkbox('is_active'); 
and db is_active field to :  int(1).

Could you confirm if this is the recommended way to handle 0/1 values in cakephp 2.x ?




Thanks,

Andras Kende

No comments: