CakePHP hasAndBelongsToMany (HABTM) Delete Connection Entry

I have a HABTM connection between users and locations. Both models have a corresponding set of variables $hasAndBelongsToMany .

When I manage user locales, I want to remove the relationship between the user and the location, but not the location. Obviously, this location may belong to other users. I would expect the following code to only delete the connection table entry provided to the HABTM associations, but it deleted both entries.

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

However, I'm new to CakePHP, so I'm sure something is missing. I tried to set the cascade to false and reorder the model using User, User-> Weather, Weather-> User. Bad luck.

Thanks in advance for your help.

+4
source share
1 answer

Not quite sure how Weather is associated with your models, so I’ll just go over to the traditional names, LocationsUser is the join table. This should remove all associations between the user with id $id and any locations:

 $this->User->LocationsUser->deleteAll(array('LocationsUser.user_id' => $id), false); 

Notice that the code snippet is missing the closing code snippet.

+8
source

Source: https://habr.com/ru/post/1309839/


All Articles