Thursday, May 24, 2012

Re: cakephp 2 tinyint(1) return as boolean changes

just use casting for this and you will be fine:

echo (int)$car['Car']['is_active']; 




Am Donnerstag, 24. Mai 2012 10:55:57 UTC+2 schrieb Andras Kende:
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

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: