CakePHP hasAndBelongsToMany (HABTM) 删除加入记录

发布于 2024-09-01 06:57:42 字数 405 浏览 5 评论 0原文

我在用户和位置之间有 HABTM 关系。两个模型都有适当的 $hasAndBelongsToMany 变量集。

当我管理用户位置时,我想删除用户和位置之间的关联,但不删除位置。显然这个位置可能属于其他用户。我希望以下代码仅删除提供 HABTM 关联的连接表记录,但它删除了两条记录。

$this->Weather->deleteAll(array('Weather.id' => $this->data['weather_ids'], false);

然而,我是 CakePHP 的新手,所以我确信我错过了一些东西。我尝试将级联设置为 false 并使用用户、用户->天气、天气->用户更改模型顺序。运气不好。

预先感谢您的任何帮助。

I have a HABTM relationship between Users and Locations. Both Models have the appropriate $hasAndBelongsToMany variable set.

When I managing User Locations, I want to delete the association between the User and Location, but not the Location. Clearly this Location could belong to other users. I would expect the following code to delete just the join table record provided the HABTM associations, but it deleted both records.

$this->Weather->deleteAll(array('Weather.id' => $this->data['weather_ids'], false);

However, I am new to CakePHP, so I am sure I am missing something. I have tried setting cascade to false and changing the Model order with User, User->Weather, Weather->User. No luck.

Thanks in advance for any help.

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

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

发布评论

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

评论(1

青巷忧颜 2024-09-08 06:57:42

不太确定 Weather 与您的模型有何关系,因此我将使用传统名称,LocationsUser 是连接表。这应该会删除 ID 为 $id 的用户与任何位置之间的所有关联:

$this->User->LocationsUser->deleteAll(array('LocationsUser.user_id' => $id), false);

另请注意,您的代码片段中缺少右括号。

Not quite sure how Weather is related to your models, so I'll just go with the traditional names, LocationsUser is the joining table. This should delete all associations between the user with id $id and any locations:

$this->User->LocationsUser->deleteAll(array('LocationsUser.user_id' => $id), false);

Notice also that you're missing a closing bracket in your code snippet.

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