Field type tinyint(1) would not save values other than 0 and 1 in CakePHP

Just a quick note. I had a “is_active” field in my users table which i had been using to store boolean 0 and 1 values. Later, i mean today, i decided to save a new value -1 to this field but when i did try, it would’t save -1 and would always save it as 1. After looking at a few blog posts around the web i found that in CakePHP every time you try to save a value other that 0 or 1 into a tinyint(1) field it would save it as 1 (as according to Nate, “Cake interprets MySQL tinyint(1) fields as virtual boolean fields, which can only have a value of 0 or 1 (true or false). Use a different column type if you don’t want this behavior”. You could find related more explanation here https://trac.cakephp.org/ticket/3903 and https://trac.cakephp.org/ticket/1253). So the known best solution so far is that you change the field id to tinyint(2) instead and it should start saving anything between -128 and 127.

Also, another quick note on changing the table structure. Although some folk around there had advised to clear the model cache app/tmp/cache/models) just after manually changing your table structure and before testing the new changes reflect it did work as expected for me even without clearing the cache (may be because i was on debug mode=2). So anyway, if you find sometimes that even after changing the table structure changes would not reflect try clearing model cache in app/tmp/cache/models.

As mentioned at beginning, just a quick note! :). Anyways full credit goes to this post and people there for this quick note of mine ;).

Leave a Reply