0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Clark
  • 2,083
  • 6
  • 32
  • 47
  • 2
    Can you post the Model association code for User & Project. – Leo Dec 15 '10 at 18:40
  • 2
    Please, use `return $this->delete($project_id);` instead of that redundant 6 line construct you have there. :) – deceze Dec 16 '10 at 01:45

1 Answers1

1

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

MichaelC
  • 243
  • 2
  • 8