cakephp 3.x Saving Nested (deep) Association

浪子不回头ぞ 提交于 2019-12-01 21:21:21

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/42020863/cakephp-3-x-saving-nested-deep-association

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!