在CakePHP 3中保存belongsToMany关联

时间:2015-04-02 12:30:35

标签: php cakephp model save associations

我在保存belongsToMany关联时遇到了问题,保存的数据是现有实体和新实体的组合。

数据数组如下:

$data = [
    'tags' => [
        ['label' => 'Some new tag'],
        ['label' => 'Another new tag'],
        '_ids' => [1, 2] 
    ]
];

这两种保存此关联的方法是不是可以一起使用?

我尝试过创建一个数据数组,其中每个实体都是自己的数组项,如下所示:

$data = [
    'tags' => [
        ['label' => 'Some new tag'],
        ['label' => 'Another new tag'],
        ['id' => 1], 
        ['id' => 2] 
    ]
];

但是当它只是ids时,这不会产生新的联想。我是否需要首先处理新实体的创建,然后使用[' _ids']方法创建关联,或者我的数据数组是否有问题?

1 个答案:

答案 0 :(得分:1)

自提交https://github.com/cakephp/cakephp/commit/0333639025780d2400b1888d0a2d1cab6c76d37a起,现在可以使用秒$ data数组结构。