CakePHP TINYINT(1) – Won’t accept values that are not 0 or 1.

This was an issue the cropped up while I was developing a web application with CakePHP. While designing on of my MySQL tables, I decided to use an unsigned TINYINT(1) column, simply because I was only going to be storing integer values between 0 and 9.

Unfortunately, things started to go awry, as I soon found out that CakePHP was basically refusing to increment my column to anything above 1. I even tried manually setting the new number, just to see if that would make a difference. Nope!

After pulling my hair and searching for that “silly mistake” that I thought I had made; I decided to head over to Stack Overflow. It was there that I was told that CakePHP treats TINYINT(1) columns as boolean columns. i.e. If you set the column to TINYINT(1), CakePHP is only going to allow you to store 0s or 1s in it.

Why?

Well, despite the frustration that this “quirk” had caused me, the answer that I received made sense. When you develop something using a framework such as CakePHP, you basically have to accept the fact that the framework in question will make certain design decisions for you. i.e. The framework’s goal is to help you speed up development. If that means overriding default behavior, then so be it. In this case, it seems as though the developers of CakePHP felt that forcing TINYINT(1) to act as a boolean column was the best approach, as MySQL’s lack of support for a true boolean column has been a pain for many developers in recent times.

PS: If you switch the TINYINY(1) to TINYINT(2) or TINYINT(3), it will work. Just make sure that you clear the modal cache before you check to see if the issue has been resolved.

Hopefully, you found this article before you decided to bin your computer!