Not signed in (Sign In)
Welcome Guest!
Want to take part in these discussions? If you have an account, sign in now.
If you don't have an account, apply for one now.
    • CommentAuthorJCC
    • CommentTimeApr 17th 2008
     

    Hi,

    I have a many-to-many association between Role and Permission.

    Now everything works fine when creating a new Role, but when updating/editing one I end up with duplicates in the junction table permissions_roles.

    Here's the controller method:

    function edit()
    {
        if (empty($this->params['id'])){
         $this->redirectToAction('listing');
        }
        if(!empty($this->params['role']) && !empty($this->params['id'])){  
            $this->Role->setAttributes($this->params['role']);
    
            if (!empty($this->params['role']['permission_id']))
                $this->role->permission->set($this->Permission->find($this->params['role']['permission_id']));
    
            if($this->Request->isPost() && $this->role->save()){
                $this->flash['notice'] = $this->t('Role was successfully updated.');
                $this->redirectTo(array('action' => 'show', 'id' => $this->role->getId()));
            }
        }
    }
    

    Now when the statement $this->role->permission->set($this->Permission->find($this->params['role']['permission_id'])); is executed it creates the entries in the joining table.

    But also, when save() is executes, it also creates the entries in the joining table leading to 2 sets for every join.

    On another note, it doesn't notice if I remove permissions from the role. Should I need to perform a delete permissions from role before adding the ones selected (and how is this achieved).

    Kind regards,

    John

      dupes.JPG
    • CommentAuthorKaste
    • CommentTimeApr 17th 2008
     

    define your association with the unique-option

     $hasAndBelongsToMany = array('permissions' => array('unique'=>true));
    
    • CommentAuthorJCC
    • CommentTimeApr 18th 2008
     
    That's done the trick - Thanks!

    John
Add your comments
    Username Password
  • Format comments as