cakephp 3.x Saving Nested (deep) Association

前端 未结 1 942
暖寄归人
暖寄归人 2021-01-20 01:15

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>>

相关标签:
1条回答
  • 2021-01-20 01:34

    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
    0 讨论(0)
提交回复
热议问题