I am having a problem where I have tables users and teams, my relation is defined as below:
public $hasAndBelongsToMany = array(
'Teams' => array(
'className' => 'Team',
'joinTable' => 'teams_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'team_id',
'unique' => 'keepExisting',
'order' => array('name' => 'ASC')
)
);
Now the problem is that when I delete a user who for example belongs to team with id 1, all associations from teams_users that have id 1 disappear with it. In my unit tests I see that the delete query doesn't really care about the user_id and deletes all in my team :( following is the automated query:
DELETE `TeamsUser`
FROM `enterpriseappstore_test`.`teams_users` AS `TeamsUser`
WHERE `TeamsUser`.`team_id` = 1
This is the code responsible for deleting the user in Model:
$ok = $this->delete((int)$userId, false);
How do I delete only associations of the specific user, not team? So, from UsersController, I need to delete one user and his connections to all the teams that remain ... now for whatever reason, I am deleting user and all associations (connections from teams_users) where team_id is the same, not user_id