Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp 3.x Saving Nested (deep) Association

I have product data coming from a 3rd party service call that I then create an object from and save to my MySQL DB. My models are as follows:

'products' hasMany>> 'product_skus' hasMany>> 'product_sku_attributes'

table relationships

In my ProductsTable.php initialize() method I have:

$this->hasMany('ProductSkus', [
    'foreignKey' => 'product_no',
    'dependent' => true,
]);

In my ProductSkusTable.php initialize() method I have:

$this->hasMany('ProductSkuAttributes', [
    'foreignKey' => 'product_sku_id',
    'bindingKey' => 'id',
    'propertyName' => 'product_sku_attributes',
    'dependent' => true,
]);

My controller:

$products = TableRegistry::get('Products');
$entity = $products->newEntity($product_data[0]);
$products->save($entity, [
    'associated' => [
        'ProductSkus',
        'ProductSkus.ProductSkuAttributes',
    ]
]);

Here's is the relevant snippet from my entity debug:

'product_skus' => [
    (int) 0 => object(App\Model\Entity\ProductSkus) {

        'sku' => 'BDS1401H',
        'sku_price' => (float) 366.76,
        'sku_weight' => (float) 38.1,
        'sku_img_main' => '',
        'sku_img_large' => '',
        'sku_img_default' => false,
        'is_default' => true,
        'product_sku_attributes' => [
            (int) 0 => [
                'product_no' => (int) 23200,
                'sku' => 'BDS1401H',
                'attribute_name' => 'Front Sway Bar Links',
                'option_name' => 'Stock'
            ],
            (int) 1 => [
                'product_no' => (int) 23200,
                'sku' => 'BDS1401H',
                'attribute_name' => 'Shock Options',
                'option_name' => 'NX2 Series'
            ],
            (int) 2 => [
                'product_no' => (int) 23200,
                'sku' => 'BDS1401H',
                'attribute_name' => 'Steering Stabilizer Options',
                'option_name' => 'Stock'
            ]
        ],
        '[new]' => true,
        '[accessible]' => [
            '*' => true,
            'id' => true
        ],
        '[dirty]' => [
            'sku' => true,
            'sku_price' => true,
            'sku_weight' => true,
            'sku_img_main' => true,
            'sku_img_large' => true,
            'sku_img_default' => true,
            'is_default' => true,
            'product_sku_attributes' => true
        ],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'ProductSkus'

    },
    (int) 1 => object(App\Model\Entity\ProductSkus) { ...

I doubled checked, and all my fields are set as accessible in my table entity classes. Also, at this point I'm only trying to save one product record for simplicity, hence $products->newEntity().

My data is saving to 'products' and 'product_skus' tables without problem, but not to 'product_sku_products'. Can anyone see what the problem is? Is it because I'm not using the same foreignKey?

Please let me know what else I can provide for clarity.

like image 214
stoff Avatar asked Feb 03 '17 09:02

stoff


1 Answers

The product_sku_attributes data is not being marshalled, it's still an array of arrays, and not an array of entities, hence it's not being saved.

Just like when saving entities, creating/patching them with associated data by default only works for first level associations. Deeper nested associations require to specify them via the associated option, ie:

$entity = $products->newEntity($product_data[0], [
    'associated' => [
        'ProductSkus.ProductSkuAttributes'
    ]
]);

$products->save($entity, [
    'associated' => [
        'ProductSkus.ProductSkuAttributes'
    ]
]);

See also

  • Cookbook > Database Access & ORM > Saving Data > Converting Request Data into Entities
  • Cookbook > Database Access & ORM > Saving Data > Saving Associations
like image 140
ndm Avatar answered Sep 30 '22 01:09

ndm