delete() 方法的错误行为

发布于 2024-10-07 22:26:39 字数 1964 浏览 0 评论 0原文

我有三个模型: 项目, 用户, 项目用户。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永言不败 2024-10-14 22:26:39

CakePHP 默认情况下会级联删除,这意味着它将尝试删除与您要删除的记录相关的所有数据。

为了防止这种行为,请将 Product 模型中的行更改为:

$result = $this->delete($project_id, false);

另外,正如 @deceze 提到的, $this->delete() 将根据删除的成功返回 true/false,因此可以安全地让您的整个功能是:

function remove($project_id, $user_id)
{ 
    // do any checks for $user_id here...
    // ...
    $result = $this->delete($project_id, false);
    return $result;
}

有关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:

$result = $this->delete($project_id, false);

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:

function remove($project_id, $user_id)
{ 
    // do any checks for $user_id here...
    // ...
    $result = $this->delete($project_id, false);
    return $result;
}

More information on delete() here: http://book.cakephp.org/view/1036/delete

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文