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

标签 cakephp cakephp-3.0

产品属于ToMany 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作为可访问属性。

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。

关于cakephp - 如何在CakePHP 3.x中保存belongsToMany以进行编辑操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29190707/

相关文章:

php - CakePHP 3.x 数据库迁移插件 : Is there a way to Change a table field?

mysql - CakePHP 3,计数/分组查找结果?

php - cakePHP 表单不向数据库发送数据

cakephp - 多个路由前缀+登录操作不起作用

php - 克隆实体和 CakePHP 3 中的所有相关实体

cakephp - 蛋糕 3 : How to add new entity to database with primaryKey set?

php - 如何在Cakephp3复合主键中插入空记录?

php - 在 CakePHP 3 中覆盖默认的 CSS 和 JS

jquery - 由于某些验证而保存失败时,动态添加的表单字段会丢失

php - Cakephp 3.4 总是将主键保存为 NULL