CakePHP hasAndBelongsToMany (HABTM) Delete Joining Record

天涯浪子 提交于 2019-12-19 08:20:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/2838732/cakephp-hasandbelongstomany-habtm-delete-joining-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!