Error saving associated data

Hi, I have 2 table which has relations

CREATE TABLE `address_headers` (
  `id` bigint(20) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `company_id` bigint(20) NOT NULL,
  `created_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `no_of_addresses` int(11) DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2nd Table

CREATE TABLE `addresses` (
  `id` int(11) NOT NULL,
  `user_id` bigint(11) NOT NULL,
  `company_id` bigint(11) NOT NULL,
  `address_header_id` int(11) NOT NULL,
  `name` varchar(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Code:-

$data = [
                'user_id' => 1,
                'company_id' => 4,
                'addresses' => [
                    'user_id' => 1,
                    'company_id' => 4,
                    'name' => 'Aneh Thakur'
                ]
            ];

            $entity = $this->AddressHeaders->newEntity();
            $entity = $this->AddressHeaders->patchEntity($entity, $data, [
                'associated' => ['Addresses']
            ]);
            if($this->AddressHeaders->save($entity)){
                echo "save";
            }else{
                pr($entity->errors());
            }

After run above it on create an entry on address header table but address table is empty.

Why wouldn’t you just save the Address first, followed by the AddressHeader? Then you can more easily debug if you find errors.

Thanks for the reply but it was not working. If I try to save address with static address_header_id it save without any error and also if I try to get save data from address_header and contain address i get data issue only in saving when I try to save data associated

Are you trying to create a circular reference? It looks like Address_header is related to Address via “no_of_addresses” the way you use it, and Address is related to Address_header via “address_header_id”.
This will get you in trouble. You should have only 1 (foreign key) relationship betwee one and the other to prevent problems. If you use “address_header_id” as the FK relation to Address, then “no_of_addresses” should just remain a number.
That means you would first create an Address_header, and then an Address.
If you get error messages, please copy them and paste them here so it is clear what errors you are running into.

you probably have (because you didnt post your relationship) hasOne or belongsTo in that case you need to

https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-belongsto-associations
https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-hasone-associations

named with the singular, underscored version of the association name

so
its address without es at the end

Hi, I get same the result, here is my code

AddressHeadersTable
$this->hasMany(‘Addresses’, [
‘foreignKey’ => ‘address_header_id’
]);

AddressesTable
$this->belongsTo(‘AddressHeaders’, [
‘foreignKey’ => ‘address_header_id’,
]);

Controller Code
$data = [
‘user_id’ => 1,
‘company_id’ => 4,
‘address’ => [
‘user_id’ => 1,
‘company_id’ => 4,
‘name’ => ‘Aneh’
]
];

        $entity = $this->AddressHeaders->newEntity();
        $entity = $this->AddressHeaders->patchEntity($entity, $data, [
            'associated' =>['Addresses']
        ]);
        if($this->AddressHeaders->save($entity)){
            echo "save";
        }else{
            pr($entity->errors());
        }

I also remove column no_of_addresses for any confusion. We need to save multiple address under one address header id. when i run above code it create entery in address_header but did not make any entry in addresses table.

ah you have hasMany so its as it was addresses but it needs to be nested array as in book
https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-hasmany-associations