cakephpforeign-keyscakephp-3.xcakephp-bake

Cakephp 3 - make bake command require presence for foreign keys


I have a table with a foreign key defined in it. After running the cake bake command, I got correctly generated the table file. That is, the references ( hasMany, belongsTo, etc. ) are included in the file. Al required fields defined as not null in the database structure other than the ones defined as foreign keys are generated as to "requirepresence". But, the ones that are foreing keys are not included as required to have presence ( so I have to manually add them ).

The question is : is this how the cake bake command supposed to work ? or I am missing something in the way ?

Thanks. Regards. Facundo.


Solution

  • Foreign keys are not marked as to be validated as present, because the ORM automatically generates/inserts foreign key values after the validation stage when saving associated records (given that you stick to the conventions), ie foreign key values are in fact not neccessarily required.

    Instead of baking validation rules for requiring foreign keys, only application rules are baked, which ensure that the given foreign key values exist in the associated table.

    So the answer is yes, this is currently how the Bake shell is supposed to work.

    See also