Search code examples
mysqlcakephpforeign-keysbelongs-to

CakePHP - Allow null value for belongsTo foreign key


I have a table called notifications in my DB and a table called notes. Notifications has a foreign key reference on notes, I want CakePHP to recognize that this value can be null, do I need to set anything up in my model for this to take effect? Or only the constraint on the DB is required?


Solution

  • I do this fairly frequently. The only problem that you'll run into is that Cake stores empty values as empty strings which is bad for foreign keys. I wrote a simple, stupid NullableBehavior to fix that.