如何保存重复记录使用belongsToMany?

Posted

技术标签:

【中文标题】如何保存重复记录使用belongsToMany?【英文标题】:How to save duplicate records use belongsToMany? 【发布时间】:2019-09-05 12:57:03 【问题描述】:

我有 2 个模型:模板和模块。两个模型都有关联 belongsToMany 到另一个。现在我想保存几个与模板相关的模块(一个模板可以多次使用模块)。我还在连接表中有额外的字段(位置)。

当我准备好我的实体来保存一切时,一切正常。我在“模块”中得到了重复的记录,但是保存后连接表中只有唯一的记录。

准备好要保存的实体:


AdminTheme\Model\Entity\Template Object
(
    [name] => t3
    [description] => text3
    [modules] => Array
        (
            [0] => AdminTheme\Model\Entity\Module Object
                (
                    [id] => 2
                    [name] => TextBox
                    [default_conf] => Array
                        (
                        )

                    [is_active] => 1
                    [global_alias] => 
                    [description] => txt2
                    [created] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2019-09-05T10:48:04+02:00
                            [timezone] => Europe/Warsaw
                            [fixedNowTime] => 
                        )

                    [modified] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2019-09-05T14:31:23+02:00
                            [timezone] => Europe/Warsaw
                            [fixedNowTime] => 
                        )

                    [ico] => format_align_justify
                    [_joinData] => Cake\ORM\Entity Object
                        (
                            [position] => 4
                            [[new]] => 1
                            [[accessible]] => Array
                                (
                                    [*] => 1
                                )

                            [[dirty]] => Array
                                (
                                    [position] => 1
                                )

                            [[original]] => Array
                                (
                                )

                            [[virtual]] => Array
                                (
                                )

                            [[hasErrors]] => 
                            [[errors]] => Array
                                (
                                )

                            [[invalid]] => Array
                                (
                                )

                            [[repository]] => ModulesTemplates
                        )

                    [[new]] => 
                    [[accessible]] => Array
                        (
                            [name] => 1
                            [default_conf] => 1
                            [is_active] => 1
                            [global_alias] => 1
                            [description] => 1
                            [ico] => 1
                            [created] => 1
                            [modified] => 1
                            [templates] => 1
                        )

                    [[dirty]] => Array
                        (
                            [_joinData] => 1
                        )

                    [[original]] => Array
                        (
                        )

                    [[virtual]] => Array
                        (
                        )

                    [[hasErrors]] => 
                    [[errors]] => Array
                        (
                        )

                    [[invalid]] => Array
                        (
                        )

                    [[repository]] => AdminTheme.Modules
                )

            [1] => AdminTheme\Model\Entity\Module Object
                (
                    [id] => 1
                    [name] => NarrowHeader
                    [default_conf] => Array
                        (
                            [title] => text
                        )

                    [is_active] => 1
                    [global_alias] => 
                    [description] => txt1
                    [created] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2019-08-30T08:27:12+02:00
                            [timezone] => Europe/Warsaw
                            [fixedNowTime] => 
                        )

                    [modified] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2019-09-05T14:31:23+02:00
                            [timezone] => Europe/Warsaw
                            [fixedNowTime] => 
                        )

                    [ico] => format_align_justify
                    [_joinData] => Cake\ORM\Entity Object
                        (
                            [position] => 6
                            [[new]] => 1
                            [[accessible]] => Array
                                (
                                    [*] => 1
                                )

                            [[dirty]] => Array
                                (
                                    [position] => 1
                                )

                            [[original]] => Array
                                (
                                )

                            [[virtual]] => Array
                                (
                                )

                            [[hasErrors]] => 
                            [[errors]] => Array
                                (
                                )

                            [[invalid]] => Array
                                (
                                )

                            [[repository]] => ModulesTemplates
                        )

                    [[new]] => 
                    [[accessible]] => Array
                        (
                            [name] => 1
                            [default_conf] => 1
                            [is_active] => 1
                            [global_alias] => 1
                            [description] => 1
                            [ico] => 1
                            [created] => 1
                            [modified] => 1
                            [templates] => 1
                        )

                    [[dirty]] => Array
                        (
                            [_joinData] => 1
                        )

                    [[original]] => Array
                        (
                            [_joinData] => Cake\ORM\Entity Object
                                (
                                    [position] => 5
                                    [[new]] => 1
                                    [[accessible]] => Array
                                        (
                                            [*] => 1
                                        )

                                    [[dirty]] => Array
                                        (
                                            [position] => 1
                                        )

                                    [[original]] => Array
                                        (
                                        )

                                    [[virtual]] => Array
                                        (
                                        )

                                    [[hasErrors]] => 
                                    [[errors]] => Array
                                        (
                                        )

                                    [[invalid]] => Array
                                        (
                                        )

                                    [[repository]] => ModulesTemplates
                                )

                        )

                    [[virtual]] => Array
                        (
                        )

                    [[hasErrors]] => 
                    [[errors]] => Array
                        (
                        )

                    [[invalid]] => Array
                        (
                        )

                    [[repository]] => AdminTheme.Modules
                )

            [2] => AdminTheme\Model\Entity\Module Object
                (
                    [id] => 1
                    [name] => NarrowHeader
                    [default_conf] => Array
                        (
                            [title] => text
                        )

                    [is_active] => 1
                    [global_alias] => 
                    [description] => txt1
                    [created] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2019-08-30T08:27:12+02:00
                            [timezone] => Europe/Warsaw
                            [fixedNowTime] => 
                        )

                    [modified] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2019-09-05T14:31:23+02:00
                            [timezone] => Europe/Warsaw
                            [fixedNowTime] => 
                        )

                    [ico] => format_align_justify
                    [_joinData] => Cake\ORM\Entity Object
                        (
                            [position] => 6
                            [[new]] => 1
                            [[accessible]] => Array
                                (
                                    [*] => 1
                                )

                            [[dirty]] => Array
                                (
                                    [position] => 1
                                )

                            [[original]] => Array
                                (
                                )

                            [[virtual]] => Array
                                (
                                )

                            [[hasErrors]] => 
                            [[errors]] => Array
                                (
                                )

                            [[invalid]] => Array
                                (
                                )

                            [[repository]] => ModulesTemplates
                        )

                    [[new]] => 
                    [[accessible]] => Array
                        (
                            [name] => 1
                            [default_conf] => 1
                            [is_active] => 1
                            [global_alias] => 1
                            [description] => 1
                            [ico] => 1
                            [created] => 1
                            [modified] => 1
                            [templates] => 1
                        )

                    [[dirty]] => Array
                        (
                            [_joinData] => 1
                        )

                    [[original]] => Array
                        (
                            [_joinData] => Cake\ORM\Entity Object
                                (
                                    [position] => 5
                                    [[new]] => 1
                                    [[accessible]] => Array
                                        (
                                            [*] => 1
                                        )

                                    [[dirty]] => Array
                                        (
                                            [position] => 1
                                        )

                                    [[original]] => Array
                                        (
                                        )

                                    [[virtual]] => Array
                                        (
                                        )

                                    [[hasErrors]] => 
                                    [[errors]] => Array
                                        (
                                        )

                                    [[invalid]] => Array
                                        (
                                        )

                                    [[repository]] => ModulesTemplates
                                )

                        )

                    [[virtual]] => Array
                        (
                        )

                    [[hasErrors]] => 
                    [[errors]] => Array
                        (
                        )

                    [[invalid]] => Array
                        (
                        )

                    [[repository]] => AdminTheme.Modules
                )

        )

    [user_id] => 3
    [amount_modules] => 3
    [[new]] => 1
    [[accessible]] => Array
        (
            [name] => 1
            [user_id] => 1
            [description] => 1
            [use_amount] => 1
            [created] => 1
            [modified] => 1
            [user] => 1
            [amount_modules] => 1
            [modules] => 1
        )

    [[dirty]] => Array
        (
            [name] => 1
            [description] => 1
            [modules] => 1
            [user_id] => 1
            [amount_modules] => 1
        )

)

尽管上面的实体有 3 个项目,但保存后我在连接表中只有 2 条记录。

【问题讨论】:

在您创建和保存实体的位置发布代码。 【参考方案1】:

您应该注意在 cakephp 中保存或关联保存具有 id 的实体或数据,它不会保存但会更新。因此,对于您的数据,有两个具有相同 id 的对象,在这种情况下,它将插入第一个对象,并且当第一个对象遇到相同的对象 id 时,它将被更新。

所以您的记录总共只有 2 行。

【讨论】:

以上是关于如何保存重复记录使用belongsToMany?的主要内容,如果未能解决你的问题,请参考以下文章

在 CakePHP 3 中保存多个 belongsToMany 关联

如何在 CakePHP 3.x 中保存 belongsToMany 以进行编辑操作?

CakePHP 3 保存 BelongsToMany 关联未知类型“”错误

Laravel:保存/附加/同步自定义枢轴模型(belongsToMany)

在 CakePHP 3 中保存 belongsToMany 关联

根据子表中的“当前”值过滤 belongsToMany 记录