cakephp 3.x 保存嵌套(深层)关联 [英] cakephp 3.x Saving Nested (deep) Association

查看:23
本文介绍了cakephp 3.x 保存嵌套(深层)关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自 3rd 方服务调用的产品数据,然后我从中创建一个对象并将其保存到我的 MySQL 数据库中.我的模型如下:

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'

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

表关系

在我的 ProductsTable.php initialize() 方法中:

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

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

在我的 ProductSkusTable.php initialize() 方法中:

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

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

我的控制器:

$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(AppModelEntityProductSkus) {

        '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(AppModelEntityProductSkus) { ...

我仔细检查了一遍,我的所有字段都在我的表实体类中设置为可访问.此外,在这一点上,为了简单起见,我只想保存一个产品记录,因此 $products->newEntity().

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().

我的数据可以毫无问题地保存到products"和product_skus"表,但不能保存到product_sku_products".任何人都可以看到问题是什么?是不是因为我没有使用相同的外键?

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.

推荐答案

product_sku_attributes 数据没有被编组,它仍然是一个数组数组,而不是一个实体数组,因此它不是已保存.

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.

就像保存实体一样,创建/修补它们与关联数据默认仅适用于第一级关联.更深的嵌套关联需要通过 associated 选项指定它们,即:

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'
    ]
]);

另见

这篇关于cakephp 3.x 保存嵌套(深层)关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆