问题描述
我在用户和位置之间建立了 HABTM 关系.两个模型都有适当的 $hasAndBelongsToMany
变量集.
I have a HABTM relationship between Users and Locations. Both Models have the appropriate $hasAndBelongsToMany
variable set.
当我管理用户位置时,我想删除用户和位置之间的关联,而不是位置.显然这个位置可能属于其他用户.我希望以下代码仅删除提供 HABTM 关联的连接表记录,但它删除了两条记录.
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);
然而,我是 CakePHP 的新手,所以我确定我遗漏了一些东西.我尝试将级联设置为 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.
在此先感谢您的帮助.
推荐答案
不太确定 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:
$this->User->LocationsUser->deleteAll(array('LocationsUser.user_id' => $id), false);
另请注意,您的代码片段中缺少一个右括号.
Notice also that you're missing a closing bracket in your code snippet.
这篇关于CakePHP hasAndBelongsToMany (HABTM) 删除加入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!