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

时间:2015-03-22 03:40:58

标签: cakephp cakephp-3.0

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',
    ]);
}

当我通过Product修改products/edit/{id}时,我会提供$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,
    ];
}

1 个答案:

答案 0 :(得分:2)

需要将product_circles添加为Product实体类中的可访问属性。

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表示赞同。