cakephpcakephp-3.xcakephp-bake

cakephp 3: bake adds undesired rule "$rules->isUnique(['email'])"


I am baking a table that saves all the attempts to login from my customers, and by some reason the bake command adds this rule.

$rules->isUnique(['email'])

This is undesired, because I want to save all the data sent during each loging attempt.

It took me a while to find why there was an error when I was trying to save many records with the same email. in my db this field was not marked as unique but bake decided to apply that rule. The question is if this is a bug or if there is a designed way to avoid that undesired behavior.


Solution

  • This bake behaviour of adding of $rules->isUnique() for fields that happen to have the name email is a "feature". (But it is very annoying when you don't want the email to be unique.)

    Congratulations on tracking down your problem!

    The best solution is to comment out the offending line of code in the XXXsTable.php file - or if you are definitely sure you will never need it, just delete the line.

    Changing the name of the field to something different from email is probably not a good idea as there was probably a good reason you chose that name in the first place.

    Bear in mind that all the files created by bake are just a starting point and you will probably need to do quite a bit of editing of them to implement your application.