Project:
var $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'projects_users',
'foreignKey' => 'project_id',
'associationForeignKey' => 'user_id',
'with' => 'ProjectsUser',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
User:
var $hasAndBelongsToMany = array(
'Project' => array(
'className' => 'Project',
'joinTable' => 'projects_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'project_id',
'with' => 'ProjectsUser',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
ProjectsUser
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Project' => array(
'className' => 'Project',
'foreignKey' => 'project_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'ProjectsUsersRole' => array(
'className' => 'ProjectsUsersRole',
'foreignKey' => 'projects_users_role_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
On 15 дек, 20:04, cricket <[email protected]> wrote:
> 2010/12/15 Andrew Hodirevski <[email protected]>:
>
>
>
>
>
>
>
>
>
> > Bump. Anyone can help me?
>
> > On 13 дек, 12:41, Andrew Hodirevski <[email protected]> wrote:
> >> Hello!
> >> 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?
>
> Can you confirm that the integer in the 2nd query (the 4 in your
> example) is the user_id and not the project_id? Please post the HABTM
> arrays for these two models.
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en