I am trying to save data with associations from array
I have tried to follow an example from
https://book.cakephp.org/4/en/orm/saving-data.html, Converting HasMany Data
[details="Summary"]
$data = [
'title' => 'My Title',
'body' => 'The text',
'comments' => [
['id' => 1, 'comment' => 'Update the first comment'],
['id' => 2, 'comment' => 'Update the second comment'],
['comment' => 'Create a new comment'],
],
];
[/details]
I cannot find a working example with Google.
I have 2 tables users and addresses as defined in
https://github.com/uvauser/test
When I try following in any controller action:
$this->loadModel(‘Users’);
$data = [
'email' => 'test@test.nl',
'password' => 'Tdsaw3cds32',
'Addresses' => [
[
'street' => 'My Street',
'house_number' => 23,
'postal_code' => '1234ab',
'city' => 'My City',
'country' => 'My Country'
]
],
];
// Trial 1
$user1 = $this->Users->newEntity($data, [
'associated' => ['Addresses' =>['validate'=>false]]
]);
$this->Users->save($user1);
// Trial 2
$user2 = $this->Users->newEmptyEntity();
$entity = $this->Users->patchEntity($user2, $data, [
'associated' => ['Addresses' =>['validate'=>false]]
]);
$this->Users->save($user2);
It does not save.
Can somebody give me a full working example