Adding many-to-many joins in CakePHP with newEntity

To allow CakePHP 3 to add entries to many-to-many join tables when creating or patching entities, the names of the associations must be added to the array of accessible concrete properties on the model.

The documentation does mention this fact eventually, as a note in the “Patching HasMany and BelongsToMany” section, but not as part of the description of how to create new entities. None of the examples in the “Converting BelongsToMany Data” section mention that they will actually fail as written when creating or patching entities.

The solution—adding a 'tags' => true  entry to the $_accessible  property of Articles—is straightforward, but I felt it was far from obvious. Until this point I had never come across any indication that there were situations in which Cake would treat associations as concrete properties.

Leave a Reply

Your email address will not be published. Required fields are marked *