Cake php 4 saving data with Associations from array

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

Your data array keys should map to the properties of the entity. So it is likely that ‘Addresses’ should be ‘addresses’ to work properly.

When I’m having trouble getting an entity to patch and save properly I’ll often do a query to get the structure I’m trying to save, then debug that. If I can then reproduce that structure with my patching process, it will save properly.

You can also do entity->toArray() to see your structure as an array rather than as objects.

Thanks
It wotks now. :slight_smile: