Cakephp 3-具有_joinData的belongsToMany [英] Cakephp 3 - belongsToMany with _joinData

查看:48
本文介绍了Cakephp 3-具有_joinData的belongsToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过联接表中的其他数据来实现bolongsToMany关联。联接表包含用于外键的列和一个附加列'context'

I try to realize a bolongsToMany association with additional data in the join table. The join table has columns for the foreign keys and an additional column 'context'

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说:

I expect the context beeing saved in the join table, but it doesn't. The debgger says:

/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'

}

我的类别和上下文在哪里。我的代码有什么问题?

Where's my category and context. What's wrong with my code?

推荐答案

如果要附加联接数据,则需要将其放入嵌套数组中。

If you want to attach join data it needs to be in a nested array.

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

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

It has to be a nested array so that you could attached multiple records (if you wanted too).

这篇关于Cakephp 3-具有_joinData的belongsToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆