Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在CakePHP 3.x中保存belongToMany以进行编辑操作?_Cakephp_Cakephp 3.0 - Fatal编程技术网

如何在CakePHP 3.x中保存belongToMany以进行编辑操作?

如何在CakePHP 3.x中保存belongToMany以进行编辑操作?,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,Products通过ProductsInCircles进入 ProductsInCirclesTable.php public function initialize(array $config) { $this->table('products_in_circles'); $this->displayField('id'); $this->primaryKey('id'); $this->belongsTo('Products', [

Products
通过
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'
    ]);
}
public function initialize(array $config)
{
    $this->table('products');
    $this->displayField('name');
    $this->primaryKey('id');
    $this->belongsToMany('ProductCircles', [
        'through' => 'ProductsInCircles',
    ]);
}
ProductsTable.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'
    ]);
}
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\u in\u circles
表中

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

如何正确地将这些内容保存并持久保存到
products\u in\u circles
表中

我当然想使用
append
选项,而不是
replace

我已经看过了文档,创建新实体的示例更清晰。我的用于编辑现有实体

此外,我无法找到在哪里打开
append
选项。看

我怀疑我在数据结构上犯了一个错误

请告知

编辑

产品实体

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
实体类中添加product\u圆作为可访问属性

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值得称赞。

是否可以在您的产品实体类中访问
product\u循环
属性?添加了产品实体代码。不,我没有产品圈作为可访问的属性。这很重要吗?好吧,看来这是必要的。谢谢,你知道在哪里使用“append”选项了吗?