model - Cakephp 3.x saving hasmany association -


in project cannot able save hasmany associated table in single save. association given follows

class productstable extends table

$this->hasmany('productimages', [             'foreignkey' => 'product_id',             'jointype' => 'inner'         ]); 

class productimagestable extends table

$this->belongsto('products', [         'foreignkey' => 'product_id',         'jointype' => 'inner'     ]); 

in controller code follows

debug($this->request->data);  \src\controller\admin\productscontroller.php (line 81)  $product = $this->products->patchentity($product, $this->request->data, [     'associated' => [ 'productimages'] ]);  debug($product);\src\controller\admin\productscontroller.php (line 86) $this->products->save($product); 

the debug result follows

 \src\controller\admin\productscontroller.php (line 81)  [     'name' => 'lorem ipsum dolor',     'short_description' => ' lorem ipsum dolor sit amet',     'description' => ' lorem ipsum dolor sit amet ',     'sku' => 'ne132w',     'price' => '51',     'product_image' => [         (int) 0 => [             'default' => (int) 0,             'image_name' => 'product1',             'real_name' => '631-1478785843.png',             'image_url' => 'http://cakephp-apps.com/mykipferl/img/products/631-1478785843.png'         ],         (int) 1 => [             'default' => '1',             'image_name' => 'product2',             'real_name' => '140-1478785850.png',             'image_url' => 'http://cakephp-apps.com/mykipferl/img/products/140-1478785850.png'         ],         (int) 2 => [             'default' => (int) 0,             'image_name' => 'product3',             'real_name' => '416-1478785856.png',             'image_url' => 'http://cakephp-apps.com/mykipferl/img/products/416-1478785856.png'         ]     ] ]  \src\controller\admin\productscontroller.php (line 86)  object(app\model\entity\product) {      'name' => 'lorem ipsum dolor',     'short_description' => ' lorem ipsum dolor sit amet',     'description' => ' lorem ipsum dolor sit amet, consectetur adipiscing elit.  ',     'sku' => 'ne132w',     'price' => (float) 51,     'product_image' => [         (int) 0 => [             'default' => (int) 0,             'image_name' => 'product1',             'real_name' => '631-1478785843.png',             'image_url' => 'http://cakephp-apps.com/mykipferl/img/products/631-1478785843.png'         ],         (int) 1 => [             'default' => '1',             'image_name' => 'product2',             'real_name' => '140-1478785850.png',             'image_url' => 'http://cakephp-apps.com/mykipferl/img/products/140-1478785850.png'         ],         (int) 2 => [             'default' => (int) 0,             'image_name' => 'product3',             'real_name' => '416-1478785856.png',             'image_url' => 'http://cakephp-apps.com/mykipferl/img/products/416-1478785856.png'         ]     ],     '[new]' => true,     '[accessible]' => [         '*' => true     ],     '[dirty]' => [         'name' => true,         'short_description' => true,         'description' => true,         'sku' => true,         'price' => true,         'product_image' => true     ],     '[original]' => [],     '[virtual]' => [],     '[errors]' => [],     '[invalid]' => [],     '[repository]' => 'products'  } 

somebody me issue?

you have use appropriate property name association, hasmany associations default plural, underscored variant of association alias, in case product_images, not product_image.

see also


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -