belongstomany relation can not create new records with patchentity, because it fails validation on the joinData looking for the keys that need to be created on the save…
articles_deals
create a new article with new deal and some joindata and save everything in one go
will fail validation deal_id and article_id missing on the joindata table. But arent these keys supposed to be created when you save the whole thing at once?
As expected when you remove the validation on the deal_id and article_id then it can save it. Maybe the BELONGSTOMANY bake should have some conditions for this to not validate those fields when the save is not part of creating new entities? Or something like that…
actually on other relation it is the same thing when you use patchentity, this has changed from version 3 I suppose, because previously you could patch associated data without getting an error for the key of the parent. being missing in the child
You can absolutely patch an entity with associated data that doesn’t include the parent ID, without generating errors. It’s impossible to say why it’s not working for you, without seeing more of your code.
Its not working because of the validation. In cakephp 3 you didnt need to change the validation, but in cakephp 4 the validation is strict and when there is no key supplied it fails. Where in cakephp 3 this was not the case. So now you need to make multiple validation rules for saving a single entity and then another set of validation for when it is saved as part of an association. Which is a step backward from 3, because it gives you more work. To have those multiple validation rules for both cases of saving the record.
There was a change to bake some time ago, which, among other things, introduced requirePresence() validation rules for foreign keys, but it has been removed again recently because of this very specific problem, however it’s not yet released, it will go into the next minor release of bake, ie 2.8.0.