Issue saving hasMany with mutlilevel association


#1

hello I dont know why the second associations is not working
public function new1()
{
$data = [
‘sku’ => ‘d45fas3adsdfsdf3’,
‘name’ => ‘blusa’,
‘price’ => 10,
‘unit’ => ‘pieza’,
‘groups’ => [
[‘name’ => ‘color’,
‘required’ => 1,
‘type’ => ‘checkbox’,
‘options’ => [‘name’ => ‘verde’,
‘value’ => 10.2,
‘available’ => 1]]
]
];
$item = $this->Items->newEntity();
$item = $this->Items->patchEntity($item, $data,[
‘associated’ => [‘Groups’=> [‘associated’=> [‘Options’]]]]);
debug($item);

    $this->Items->save($item);
    debug($item);

}`

this is the object after trying to save:
object(App\Model\Entity\Item) {

'sku' => 'd45fas3adsdfsdf3',
'name' => 'blusa',
'price' => (float) 10,
'unit' => 'pieza',
'groups' => [
	(int) 0 => object(App\Model\Entity\Group) {

		'name' => 'color',
		'required' => (int) 1,
		'type' => 'checkbox',
		'item_id' => (int) 42,
		'id' => (int) 5,
		'[new]' => false,
		'[accessible]' => [
			'name' => true,
			'required' => true,
			'description' => true,
			'type' => true,
			'order' => true,
			'item_id' => true,
			'item' => true,
			'groups_options' => true,
			'orders_details' => true
		],
		'[dirty]' => [],
		'[original]' => [],
		'[virtual]' => [],
		'[errors]' => [],
		'[invalid]' => [],
		'[repository]' => 'Groups'
	
	}
],
'id' => (int) 42,
'[new]' => false,
'[accessible]' => [
	'sku' => true,
	'name' => true,
	'description' => true,
	'brand' => true,
	'price' => true,
	'stock' => true,
	'unit' => true,
	'status' => true,
	'category_id' => true,
	'category' => true,
	'images' => true,
	'groups' => true,
	'coupons' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Items'

}
cakecos
I would really appreciate your help


#2

Did you try

$item = $this->Items->patchEntity($item, $data, [‘associated’ => [‘Groups’, ‘Groups.Options’]]);

instead of

$item = $this->Items->patchEntity($item, $data,[‘associated’ => [‘Groups’=> [‘associated’=> [‘Options’]]]]);

?