Yii Framework Forum: Boolean Field Problem - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Boolean Field Problem return string not integer Rate Topic: -----

#1 User is offline   studyyii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 06-December 12

Posted 07 December 2012 - 12:53 AM

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?
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,303
  • Joined: 04-March 10
  • Location:UK

Posted 07 December 2012 - 05:23 AM

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.
0

#3 User is offline   studyyii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 06-December 12

Posted 07 December 2012 - 10:47 AM

View PostKeith, on 07 December 2012 - 05:23 AM, said:

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.
0

#4 User is offline   studyyii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 06-December 12

Posted 07 December 2012 - 03:20 PM

View PostKeith, on 07 December 2012 - 05:23 AM, said:

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"?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users