CakePHP hasAndBelongsToMany (HABTM) 删除加入记录
我在用户和位置之间有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不太确定
Weather
与您的模型有何关系,因此我将使用传统名称,LocationsUser
是连接表。这应该会删除 ID 为$id
的用户与任何位置之间的所有关联:另请注意,您的代码片段中缺少右括号。
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:Notice also that you're missing a closing bracket in your code snippet.