Ajith
Ajith

Reputation: 2666

Cakephp 3.x saving hasmany association

In my project I cannot able to save hasmany associated table in a single save. My association is given as 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 my controller the code is as 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 is as 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 help me for this issue?

Upvotes: 0

Views: 2091

Answers (1)

ndm
ndm

Reputation: 60453

You have to use the appropriate property name for the association, which for hasMany associations is by default the plural, underscored variant of the association alias, so in your case product_images, not product_image.

See also

Upvotes: 4

Related Questions