delete() 方法的错误行为
我有三个模型: 项目, 用户, 项目用户。 ProjectsUser 表用于项目和项目之间的 HABTM 关系 用户。 这是项目模型的remove()方法:
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;
}
和项目控制器的remove()方法:
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',
// ));
}
所以,在调试时我发现Cake对delete()进行了两次查询 方法:从projects表中按project_id删除项目 下一个: 从 projects_users
删除,其中 projects_users
.user_id
= 4 这意味着如果用户有两个项目,那么在查询所有关系后 在projects_users 表中的user_id 将被删除。 我该如何解决这个问题以及为什么 Cake 通过 user_id 从projects_users 中删除 不是通过project_id?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CakePHP 默认情况下会级联删除,这意味着它将尝试删除与您要删除的记录相关的所有数据。
为了防止这种行为,请将 Product 模型中的行更改为:
另外,正如 @deceze 提到的, $this->delete() 将根据删除的成功返回 true/false,因此可以安全地让您的整个功能是:
有关delete()的更多信息,请参见:http://book.cakephp.org/view/ 1036/删除
CakePHP cascades it's deletes by default, which means that it will attempt to delete all related data to the record that you are trying to remove.
To prevent this behavior, change your line in the Product model to:
Also, as @deceze mentioned, $this->delete() will return true/false depending on the success of the delete, so it is safe to have your entire function be:
More information on delete() here: http://book.cakephp.org/view/1036/delete