studyyii
(785230)
December 7, 2012, 5:53am
1
Hi,everyone.I have a problem about active record boolean field.
My table schema sql is:
CREATE TABLE foo
(
id
int(11) NOT NULL AUTO_INCREMENT,
isbar
bool NOT NULL ,
PRIMARY KEY (id
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
I have a Foo.php as:
class Foo extends CActiveRecord
{
public function rules()
{
return array(
array('isbar','boolean'),
);
}
}
I have a $fooObject which is an instance of Foo. let
$isbar=$fooObject->isbar;
$json=json_encode($isbar);
I found that $json is a string like "0" or "1",but what I need is 0 ,1 ,false or true.
In a word, how can I let boolean property return integer or boolean?
Keith
(Kburton)
December 7, 2012, 10:23am
2
Whenever I attempt to create a boolean field in InnoDB, it creates a field of type tinyint(1). I’d recommend changing your table schema to use that type, which should be recognised as an integer by PDO.
studyyii
(785230)
December 7, 2012, 3:47pm
3
Keith:
Whenever I attempt to create a boolean field in InnoDB, it creates a field of type tinyint(1). I’d recommend changing your table schema to use that type, which should be recognised as an integer by PDO.
I have tried tinyint(1),but nothing change.Actually,when I use bool in mysql,it creates a field of type tinyint(1) too. They are the same thing to mysql.
studyyii
(785230)
December 7, 2012, 8:20pm
4
Keith:
Whenever I attempt to create a boolean field in InnoDB, it creates a field of type tinyint(1). I’d recommend changing your table schema to use that type, which should be recognised as an integer by PDO.
Sorry!I misunderstood your meaning.After some researches,I knew this is a php ‘bug’.
Now my question become:how to detect a field is tinyint(1) type in active record?
what is the correct api for something like $fooObject->metadata->columns["isbar"]->fieldtype==="boolean"?