Cakephp 3 - 使用_joinData属于ToMany

时间:2017-12-21 11:54:22

标签: cakephp cakephp-3.0

我尝试在连接表中实现与其他数据的bolongsToMany关联。连接表具有外键列和附加列“上下文”

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);
}

我希望上下文保存在连接表中,但事实并非如此。 debgger说:

/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 个答案:

答案 0 :(得分:1)

如果要附加连接数据,则需要使用嵌套数组。

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

它必须是一个嵌套数组,以便您可以附加多个记录(如果您也需要)。