On storing boolean values in a Database (examples using Yii Framework)

Last updated:

Every now and then we need to create a boolean attribute for a database table or model (if you're using the Active Record pattern, there is almost no distinction between the two of them).

For some reason, I find it slightly tricky to get this working, so I'll write here the setups that work (on MySQL, mostly).

  • use tinyint(1) MySQL datatype.
  • use CBooleanValidator (for example: ['has_been_modified', 'boolean'], <- within your rules() method in your model, if you have an attribute called "has_been_modified")

Dialogue & Discussion