Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorJCC
     

    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
     

    define your association with the unique-option

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

    John