0

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.

Josexato
  • 50
  • 6
  • I think this is the line that decides to make unique all the "email" fields [https://github.com/cakephp/bake/blob/7356656e236bb62733f4fdd4894ce1f26f9e14f6/src/Shell/Task/ModelTask.php#L801] – Josexato Aug 29 '18 at 07:22
  • 1
    In other words - it will always add this constraint when field is named `email`. You can change that name to `uemail` and everything will work as you expect, or just remove constraint after baking. – Antoniossss Aug 29 '18 at 08:04
  • This looks to be intended behaviour by the looks of the code. Baking code is intended to speed up development, but there is nothing to stop you modifying the generated code afterwards to make it work as you need. As @Antoniossss says just remove the rule after baking. – drmonkeyninja Aug 29 '18 at 11:13

1 Answers1

1

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.

Annabel
  • 1,394
  • 14
  • 23
  • 1
    Thanks for the insight, I think bake is in deed a great tool, and now I understand that the objective is to easy to start boilerplate more than a expert system for all the possible situations, thanks! – Josexato Sep 03 '18 at 08:54