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

Posted

技术标签:

【中文标题】如何在 CakePHP 3.x 中保存 belongsToMany 以进行编辑操作?【英文标题】:how to save belongsToMany for edit action in CakePHP 3.x? 【发布时间】:2015-03-22 03:40:57 【问题描述】:

Products belongsToMany ProductCirclesProductsInCircles

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表中

$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 表中?

我当然也想使用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 实体类中是否可以访问 product_circles 属性? 添加了产品实体代码。不,我没有 product_circles 作为可访问属性。这很关键吗? 好的,看起来这是必要的。谢谢洛伦佐 你知道在哪里使用“附加”选项吗? 【参考方案1】:

需要在Product实体类中添加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,
    ];

cmets 部分归功于 Jose Lorenzo。

【讨论】:

以上是关于如何在 CakePHP 3.x 中保存 belongsToMany 以进行编辑操作?的主要内容,如果未能解决你的问题,请参考以下文章

CakePHP 3.x - 保存这些表的关联数据和表单输入

保存倍数 Cakephp 3.x

CakePHP 3.x:将一个输入保存到多个表中

CakePHP 3.x ORM get()、find() 以及如何禁用befeoreFind()?

CakePHP 3.x创建表单模板

CAKEPHP 3.x - 实体更新时出错 - 即使有值,也会声明缺少字段