在我的项目中,我无法在单个保存中保存hasmany关联表。我的关联如下:
$this->hasMany('ProductImages', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
在我的控制器中,代码如下
debug($this->request->data); \src\Controller\Admin\ProductsController.php (line 81)
$product = $this->Products->patchEntity($product, $this->request->data, [
'associated' => [ 'ProductImages']
]);
debug($product);\src\Controller\Admin\ProductsController.php (line 86)
$this->Products->save($product);
\src\Controller\Admin\ProductsController.php (line 81)
[
'name' => 'Lorem ipsum dolor',
'short_description' => ' Lorem ipsum dolor sit amet',
'description' => ' Lorem ipsum dolor sit amet ',
'sku' => 'NE132W',
'price' => '51',
'product_image' => [
(int) 0 => [
'default' => (int) 0,
'image_name' => 'product1',
'real_name' => '631-1478785843.png',
'image_url' => 'http://cakephp-apps.com/mykipferl/img/Products/631-1478785843.png'
],
(int) 1 => [
'default' => '1',
'image_name' => 'product2',
'real_name' => '140-1478785850.png',
'image_url' => 'http://cakephp-apps.com/mykipferl/img/Products/140-1478785850.png'
],
(int) 2 => [
'default' => (int) 0,
'image_name' => 'product3',
'real_name' => '416-1478785856.png',
'image_url' => 'http://cakephp-apps.com/mykipferl/img/Products/416-1478785856.png'
]
]
]
\src\Controller\Admin\ProductsController.php (line 86)
object(App\Model\Entity\Product) {
'name' => 'Lorem ipsum dolor',
'short_description' => ' Lorem ipsum dolor sit amet',
'description' => ' Lorem ipsum dolor sit amet, consectetur adipiscing elit. ',
'sku' => 'NE132W',
'price' => (float) 51,
'product_image' => [
(int) 0 => [
'default' => (int) 0,
'image_name' => 'product1',
'real_name' => '631-1478785843.png',
'image_url' => 'http://cakephp-apps.com/mykipferl/img/Products/631-1478785843.png'
],
(int) 1 => [
'default' => '1',
'image_name' => 'product2',
'real_name' => '140-1478785850.png',
'image_url' => 'http://cakephp-apps.com/mykipferl/img/Products/140-1478785850.png'
],
(int) 2 => [
'default' => (int) 0,
'image_name' => 'product3',
'real_name' => '416-1478785856.png',
'image_url' => 'http://cakephp-apps.com/mykipferl/img/Products/416-1478785856.png'
]
],
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'name' => true,
'short_description' => true,
'description' => true,
'sku' => true,
'price' => true,
'product_image' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Products'
}
有人帮我解决了这个问题吗?
答案 0 :(得分:3)
您必须为关联使用适当的属性名称,hasMany
关联默认为关联别名的复数,下划线变体,因此在您的情况下product_images
,而不是{{1 }}
另见