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, Converting HasMany Data

$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'],

I cannot find a working example with Google.
I have 2 tables users and addresses as defined in

When I try following in any controller action:


     $data = [
         'email' => '',  
        '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]]
     // Trial 2
    $user2 = $this->Users->newEmptyEntity();
    $entity = $this->Users->patchEntity($user2, $data, [
        'associated' => ['Addresses' =>['validate'=>false]]

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.

It wotks now. :slight_smile: