cakephp 3.x保存嵌套(深层)关联 [英] cakephp 3.x Saving Nested (deep) Association
问题描述
我有来自第三方服务调用的产品数据,然后我从中创建一个对象并将其保存到我的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(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) { ...
我仔细检查了一下,并且在我的表实体类中将所有字段都设置为可访问.而且,在这一点上,为了简化起见,我只想保存一个产品记录,因此是$ 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".谁能看到问题所在?是因为我没有使用相同的foreignKey吗?
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屋!