2017-12-21 28 views
1

私は、joinテーブルの追加データとのbolongsToManyの関連付けを実現しようとします。結合テーブルは、外部キーと追加の列「コンテキスト」の列Cakephp 3 - _joinDataのbelongsToMany

articlesController: 
$article = $this->Articles->patchEntity($article, $this->request->getData()); 
debug($article);die; 


and in a Plugin-Behavior: 
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) 
{ 
    $data['Categories.Categories'] = ['id' => '1', '_joinData' => ['context' => 'Tag']]; 
    debug($data); 
} 

は、私は、コンテキストが結合テーブルに保存されたbeeingて期待していたが、それはしていません。デバッガーのコメント:

/plugins/Categories/src/Model/Behavior/CategorizeableBehavior.php (line 37) 
object(ArrayObject) { 
    name => 'dfa' 
    description => 'er' 
    Categories.Categories => [ 
     'id' => '1', 
     '_joinData' => [ 
      'context' => 'Tag' 
     ] 
    ] 
} 
/src/Controller/ArticlesController.php (line 56) 
object(App\Model\Entity\Article) { 

    'name' => 'dfa', 
    'description' => 'er', 
    '[new]' => true, 
    '[accessible]' => [ 
     'name' => true, 
     'description' => true, 
     'created' => true, 
     'modified' => true 
    ], 
    '[dirty]' => [ 
     'name' => true, 
     'description' => true 
    ], 
    '[original]' => [], 
    '[virtual]' => [], 
    '[errors]' => [], 
    '[invalid]' => [], 
    '[repository]' => 'Articles' 

} 

私のカテゴリとコンテキストはどこですか?私のコードに何が問題なのですか?

答えて

1

結合データを添付する場合は、入れ子配列にする必要があります。

$data['Categories.Categories'] = [ 
     ['id' => '1', '_joinData' => ['context' => 'Tag']] 
]; 

複数のレコードを添付できるように、ネストされた配列である必要があります(必要な場合)。