如何在 CakePHP 3.x 中保存 belongsToMany 以进行编辑操作?
how to save belongsToMany for edit action in CakePHP 3.x?
Products
belongsToMany ProductCircles
通过 ProductsInCircles
.
ProductsInCirclesTable.php
public function initialize(array $config)
{
$this->table('products_in_circles');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('Products', [
'foreignKey' => 'product_id'
]);
$this->belongsTo('ProductCircles', [
'foreignKey' => 'product_circle_id'
]);
}
ProductsTable.php
public function initialize(array $config)
{
$this->table('products');
$this->displayField('name');
$this->primaryKey('id');
$this->belongsToMany('ProductCircles', [
'through' => 'ProductsInCircles',
]);
}
当我通过 products/edit/{id}
编辑 Product
时,我将提供来自 $this->request->data
的以下数据
Array
(
[name] => Piuma
[attributes_in_json] => {"size":"large"}
[rooms] => Array
(
[0] => 2
)
[styles] => Array
(
[0] => 15
[1] => 16
)
[materials] => Array
(
[0] => 27
)
[product_circles] => Array
(
[_ids] => Array
(
[0] => 2
[1] => 15
[2] => 16
[3] => 27
)
)
[associated] => Array
(
[0] => ProductCircles
)
)
以下代码没有将关联数据存入products_in_circles
table
$product = $this->Products->patchEntity($product, $this->request->data);
$product->dirty('product_circles', true);
if ($this->Products->save($product)) {
我也试过了
$product = $this->Products->patchEntity($product, $this->request->data, ['associated' => ['ProductCircles']]);
if ($this->Products->save($product)) {
和
$product = $this->Products->patchEntity($product, $this->request->data);
if ($this->Products->save($product, ['associated' => ['ProductCircles']])) {
和
$product = $this->Products->patchEntity($product, $this->request->data, ['ProductCircles']);
if ($this->Products->save($product)) {
如何正确地将它们保存并保存到 products_in_circles
table 中?
我当然也想使用 append
选项而不是 replace
。
我查看了文档,创建新实体的示例更加清晰。我的用于编辑现有实体。
此外,我找不到在哪里打开 append
选项。参见 http://book.cakephp.org/3.0/en/orm/saving-data.html#saving-belongstomany-associations
我怀疑我的数据结构有误。
请指教
编辑
产品实体
class Product extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* @var array
*/
protected $_accessible = [
'name' => true,
'attributes_in_json' => true,
'created_by' => true,
'modified_by' => true,
'prices' => true,
];
}
需要在 Product
实体 class 中添加 product_circles 作为可访问的 属性。
class Product extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* @var array
*/
protected $_accessible = [
'name' => true,
'attributes_in_json' => true,
'created_by' => true,
'modified_by' => true,
'prices' => true,
'product_circles' => true,
];
}
在评论部分感谢 Jose Lorenzo。
Products
belongsToMany ProductCircles
通过 ProductsInCircles
.
ProductsInCirclesTable.php
public function initialize(array $config)
{
$this->table('products_in_circles');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('Products', [
'foreignKey' => 'product_id'
]);
$this->belongsTo('ProductCircles', [
'foreignKey' => 'product_circle_id'
]);
}
ProductsTable.php
public function initialize(array $config)
{
$this->table('products');
$this->displayField('name');
$this->primaryKey('id');
$this->belongsToMany('ProductCircles', [
'through' => 'ProductsInCircles',
]);
}
当我通过 products/edit/{id}
编辑 Product
时,我将提供来自 $this->request->data
Array
(
[name] => Piuma
[attributes_in_json] => {"size":"large"}
[rooms] => Array
(
[0] => 2
)
[styles] => Array
(
[0] => 15
[1] => 16
)
[materials] => Array
(
[0] => 27
)
[product_circles] => Array
(
[_ids] => Array
(
[0] => 2
[1] => 15
[2] => 16
[3] => 27
)
)
[associated] => Array
(
[0] => ProductCircles
)
)
以下代码没有将关联数据存入products_in_circles
table
$product = $this->Products->patchEntity($product, $this->request->data);
$product->dirty('product_circles', true);
if ($this->Products->save($product)) {
我也试过了
$product = $this->Products->patchEntity($product, $this->request->data, ['associated' => ['ProductCircles']]);
if ($this->Products->save($product)) {
和
$product = $this->Products->patchEntity($product, $this->request->data);
if ($this->Products->save($product, ['associated' => ['ProductCircles']])) {
和
$product = $this->Products->patchEntity($product, $this->request->data, ['ProductCircles']);
if ($this->Products->save($product)) {
如何正确地将它们保存并保存到 products_in_circles
table 中?
我当然也想使用 append
选项而不是 replace
。
我查看了文档,创建新实体的示例更加清晰。我的用于编辑现有实体。
此外,我找不到在哪里打开 append
选项。参见 http://book.cakephp.org/3.0/en/orm/saving-data.html#saving-belongstomany-associations
我怀疑我的数据结构有误。
请指教
编辑
产品实体
class Product extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* @var array
*/
protected $_accessible = [
'name' => true,
'attributes_in_json' => true,
'created_by' => true,
'modified_by' => true,
'prices' => true,
];
}
需要在 Product
实体 class 中添加 product_circles 作为可访问的 属性。
class Product extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* @var array
*/
protected $_accessible = [
'name' => true,
'attributes_in_json' => true,
'created_by' => true,
'modified_by' => true,
'prices' => true,
'product_circles' => true,
];
}
在评论部分感谢 Jose Lorenzo。