tags:

views:

267

answers:

1

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  A: 

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.

deceze
Yes, sorry, the model is actually Weather locations and I used them interchangeably. Your code was spot on, I didn't realize I could reference the association Model. Here's what I had for completeness. Works just right!`$this->User->UsersWeather->deleteAll(array('UsersWeather.weather_id' => $this->data['weather_ids'], 'UsersWeather.user_id' => $this->user['User']['id']))`
Jason McCreary