joinData and new records still doesnt work properly in cakephp4?

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

$data = [“name” => “somename”, “deals” => [“0” => [“name” => “somename”, “_joinData” => [“datax” => “somedatax”]]…;

$this->Articles->patchEntity($article, $data, [
‘associated’ => ‘Deals._joinData’,
]);

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

Feels like this is a step backward from cakephp 3 imho

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.

The code I am using is

$data = [‘name’ => ‘blabla’,
‘comments’ => [‘name’ => 'blabla

$article = $this->Articles->newEmptyEntity();

$article = $this->Articles->patchEntity($article, $data);

when patchentity it will show article_id missing in the errors when you print_r($article)

maybe you havent tested it in the latest version and assume everything still works like it used to…

$validator
->integer(‘article_id’)
->requirePresence(‘article_id’, ‘create’)
->notEmptyString(‘article_id’);

Its the most standard of things using bake. You need to remove this validator or create a different set of validation otherwise its not working.

My join tables don’t have any such validation for the foreign key fields. I do have

$rules->add($rules->existsIn(['article_id'], 'Articles'));

in my buildRules functions for them.

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.

https://github.com/cakephp/bake/pull/841

1 Like

Yes, it would be easy if patchEntity would somehow know when this field will be created because it won’t be missing when the whole thing gets saved.

think this might be a problem I was having too