CakePHP hasAndBelongsToMany(HABTM)删除加入记录 [英] CakePHP hasAndBelongsToMany (HABTM) Delete Joining Record

查看:152
本文介绍了CakePHP hasAndBelongsToMany(HABTM)删除加入记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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

但是,我是CakePHP的新用户,所以我确定我缺少一些东西我已经尝试设置cascade为false和改变模型顺序与User,User-> Weather,

解决方案

不太确定天气如何与你的模型有关,所以我将使用传统名称 LocationsUser 是联接表,应删除id $ id 的用户与任何位置之间的所有关联:

  $ this-> User-> LocationsUser-> deleteAll(array('LocationsUser.user_id'=> $ id),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.

解决方案

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)删除加入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆