2015-03-22 2 views
2

Products принадлежитToMany ProductCircles через ProductsInCircles.Как сохранить ownToMany для редактирования действия в CakePHP 3.x?

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 
     ) 

) 

Следующий код не сохранить Associat эд данные в таблицу 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.

Я просмотрел документы, и этот пример более ясен для создания нового объекта. Mine предназначен для редактирования существующего объекта.

Кроме того, я не могу узнать, где я включаю опцию append. См. http://book.cakephp.org/3.0/en/orm/saving-data.html#saving-belongstomany-associations

Я подозреваю, что допустил ошибку в том, как структурированы данные.

Просьба сообщить.

EDIT

Entity Продукт

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, 
    ]; 
} 
+2

- свойство 'product_circles', доступное в вашем классе объектов Product? –

+0

добавлен Код продукта продукта. Нет, у меня нет product_circles в качестве доступного свойства. Это важно? –

+0

Хорошо, похоже, что это было необходимо. Спасибо, Lorenzo –

ответ

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

Кредит принадлежит Хосе Лоренцо в разделе комментариев.

Смежные вопросы