CakePHP保存两个模型,第二个基于第一个

发布于 2024-10-07 23:03:45 字数 834 浏览 10 评论 0原文

我有以下关联:

Group hasAndBelongsToMany User,与 groups_users 连接表。

当我创建一个新组时,我想将组的创建者添加到连接表中。

我的 $this->data 数组看起来像:

Array
     (
[Group] => Array
    (
        [name] => seyufgsduifsj
        [access] => 2
        [founder_id] => 3
        [random_key] => I6XC7uMTelpTSdq8DbtLPjqubiF7s6Pn
    )

[GroupsUser] => Array
    (
        [user_id] => 3
        [group_role_id] => 1
        [random_key] => PZsDZXcoCTHw1IuvqsfURVpPX6AcZ3r2
    )

)

我尝试使用 save() 和 saveAll(),但它只会添加组表中的组,而不是用户组关联。

所以,基本上,我有一个添加表单,用户在其中填写组名称和访问权限。然后,如果数据有效,我会向组数组(如 random_key)和连接表数组(如 user_id 和 group_role_id)添加几个字段值。我希望 Cake 首先保存该组,获取其 id,使用正确的 group_id 更新连接表数组,然后也保存第二个数组。

这是否可以直接进行,或者我是否必须编写两个后续的 save() 方法,以便第二个方法提供第一个方法中最后插入的 id?

谢谢!

I have the following association:

Group hasAndBelongsToMany User, with the groups_users join table.

When I create a new group, I want to add to the join table the founder of the group.

My $this->data array looks like:

Array
     (
[Group] => Array
    (
        [name] => seyufgsduifsj
        [access] => 2
        [founder_id] => 3
        [random_key] => I6XC7uMTelpTSdq8DbtLPjqubiF7s6Pn
    )

[GroupsUser] => Array
    (
        [user_id] => 3
        [group_role_id] => 1
        [random_key] => PZsDZXcoCTHw1IuvqsfURVpPX6AcZ3r2
    )

)

I tried with save() and saveAll(), but it would add only the group in the groups table and not the user-group association.

So, basically, I have an add form where the user fills in the group name and access. Then, if the data validates, I add a couple of more field values to the group array (like random_key) and to the join table array (like user_id and group_role_id). I want Cake to save the group first, take its id, update the join table array with the proper group_id, and then save this second array too.

Is this possible in a straight-forward way, or do I have to write two consequent save() methods, for the second one providing the last inserted id in the first one?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

◇流星雨 2024-10-14 23:03:45

您可以使用 saveAll() 函数,但需要更改 GroupsUser 数组的格式。根据 saveAll() 的文档,您的数据应如下所示

Array
     (
[Group] => Array
    (
        [name] => seyufgsduifsj
        [access] => 2
        [founder_id] => 3
        [random_key] => I6XC7uMTelpTSdq8DbtLPjqubiF7s6Pn
    )

[GroupsUser] => Array
    (
        [0] => Array
            (
               [user_id] => 3
               [group_role_id] => 1
               [random_key] => PZsDZXcoCTHw1IuvqsfURVpPX6AcZ3r2
            )
    )
)

:如果关系是GroupbelongsToGroupsUser,示例将有效,但我相信这不是你的情况

You can use saveAll() function, but you need to change the format of the GroupsUser array. According to documentation of saveAll() your data should look like this:

Array
     (
[Group] => Array
    (
        [name] => seyufgsduifsj
        [access] => 2
        [founder_id] => 3
        [random_key] => I6XC7uMTelpTSdq8DbtLPjqubiF7s6Pn
    )

[GroupsUser] => Array
    (
        [0] => Array
            (
               [user_id] => 3
               [group_role_id] => 1
               [random_key] => PZsDZXcoCTHw1IuvqsfURVpPX6AcZ3r2
            )
    )
)

Your example would work if the relation is Group belongsTo GroupsUser, but I believe this is not your case

忆依然 2024-10-14 23:03:45

也许相关模型没有通过验证。使用以下代码仅在所有模型都验证时进行保存:

$this->Group->saveAll($this->data, array('validate' => 'only'));

如果您这次没有发现您的组被保存,那么很可能是您的连接模型中的验证规则失败了。我的猜测是,您对 group_id 有一个 notEmpty 验证,您必须将其删除才能使 saveAll() 正常工作。

希望这能为您指明解决问题的方向。


编辑:你的联想有点不对劲。它应该是这样的:

Group hasMany GroupsUser
User hasMany GroupsUser
GroupRole hasMany GroupsUser
GroupsUser belongsTo Group
GroupsUser belongsTo User
GroupsUser belongsTo GroupRole

删除组和用户之间的 hasAndBelongsToMany 关联。它不再存在了。

Perhaps the related models are not passing validation. Use the following piece of code to save only if all models validate:

$this->Group->saveAll($this->data, array('validate' => 'only'));

If you don't find your Group being saved this time around, then it's most probably validation rules in your Join Model that are failing. My guess is that you have a notEmpty validation for the group_id, which you would have to remove in order for saveAll() to work.

Hope this points you in the direction of solving your problem.


EDIT: Your associations are a little off. Here's what it should be:

Group hasMany GroupsUser
User hasMany GroupsUser
GroupRole hasMany GroupsUser
GroupsUser belongsTo Group
GroupsUser belongsTo User
GroupsUser belongsTo GroupRole

Remove the hasAndBelongsToMany association between Groups and User. It doesn't exist any more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文