I have three models: Project, User, ProjectsUser. The ProjectsUser table is used for HABTM relations between Project and User. This is remove() method of Project model:
function remove($project_id, $user_id)
{
/*
* Проверяем, относится ли выбранный проект к пользователю
*/
// $data = $this->read('', $project_id);
//
// if($data['User'][0]['id'] != $user_id)
// {
// return false;
// }
/*
* Если проект принадлежит пользователю, то удалить его
*/
$result = $this->delete($project_id);
if(!$result)
{
return false;
}
return true;
}
And remove() method of Projects controller:
function remove($project_id)
{
/*
* Пробуем удалить проект
*/
$user_id = $this->Session->read('Auth.User.id');
$result = $this->Project->remove($project_id, $user_id);
/*
* Если возникли ошибки, то отправить их в буфер сообщений
о результате операций
*/
if(!$result)
{
$this->Session->setFlash('Возникли проблемы при
удалении проекта, попробуйте позже');
$this->redirect(array(
'controller' => 'projects',
'action' => 'index',
));
}
$this->Session->setFlash('Проект успешно удален');
// $this->redirect(array(
// 'controller' => 'projects',
// 'action' => 'index',
// ));
}
So, while debugging I found that Cake makes two queries for delete()
method: deleting project by project_id from projects table and the
next:
DELETE FROM projects_users
WHERE projects_users
.user_id
= 4
It means that if User have two Project then after query all relations
in projects_users table for user_id would be deleted.
How can I fix this and why Cake deletes from projects_users by user_id
not by project_id?