0
votes

I am trying to save the user but when I save I get the following error

AclNode::node() - Couldn't find Aro node identified by "Array ( [Aro0.model] => Role [Aro0.foreign_key] => ) "

I also get this error in the top

Undefined index: role_id [CORE\Cake\Model\AclNode.php, line 140]

I don't know what to do as when I added the roles it added them to aros with ease so why is it now giving me problem

I followed the guide provided by

http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html

which should be simple but it seems its not so simple.

In the Role Model

public $primaryKey = 'role_id';
public $displayField = 'role';
public $actsAs = array('Acl' => array('type' => 'requester'));

public function parentNode() {
   return null;
}

In the User Model

public $primaryKey = 'user_id';
public $displayField = 'username';

public function beforeSave($options = array()) {
        $this->data['User']['password'] = AuthComponent::password(
          $this->data['User']['password']
        );
        return true;
    }

    public $hasMany = array(
        'Role'   => array(
        'className' => 'Role',         
        'foreignKey' => 'role_id'
            )
        );

public $actsAs = array('Acl' => array('type' => 'requester'));

    public function parentNode() {
        if (!$this->id && empty($this->data)) {
            return null;
        }
        if (isset($this->data['User']['role_id'])) {
            $roleId = $this->data['User']['role_id'];
        } else {
            $roleId = $this->field('role_id');
        }
        if (!$roleId) {
            return null;
        } else {
            return array('Role' => array('id' => $roleId));
        }
    }

My Save Code for User

public function add() {                  
//Populate roles dropdownlist        
        $data = $this->User->Role->find('list', array('fields' => array('role_id', 'role')));
        $this->set('roles', $data);

        if ($this->request->is('post')) {
            $this->User->ContactDetail->create(); 
            $this->User->ContactDetail->save($this->request->data);
            $this->request->data['User']['contact_detail_id'] = $this->User->ContactDetail->id;
            $this->User->Create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
    }
2
I think when saving the user, the behavior tries to save the user's role as the parent ARO for the user's ARO, but the role_id is not being correctly passed. Is your parentNode() returning the roleId correctly? Can you debug what is returned by the parentNode() method when you save a user? The relationship defined in the User model should be User belongsTo Role. The guide that you are referring to will work if you use that code and replace'Group' with 'Role'.jimmymadon

2 Answers

1
votes

I agree with Jimmy, you should change the relationship to $belongsTo. When you save user, it tries to save aros table too so, you need to modify some changes if you don'want user to be saved in aros, only role is enough to be saved aros table (this is also explained in the tutorial), so please see the changes I made in those two models.

Role Model should be:

...
    public $hasMany = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'role_id',

        )
    );

    public $actsAs = array('Acl' => array('type' => 'requester'));

    public function parentNode() {
        return null;
    }
    ...

User Model should be:

...

    public $belongsTo = array(
        'Role' => array(
            'className' => 'Role',
            'foreignKey' => 'role_id',
        )
    );


    public function bindNode($user) {
        return array('model' => 'Role', 'foreign_key' => $user['User']['role_id']);
    }
...

As shown in the above code, public $actsAs = array('Acl' => array('type' => 'requester')); should be only on Role model, see the relationship I added and also see parentNode and bindNode function changes.

Try this and contact me if there is anything.

Hope it helps

0
votes

Based on your parentNode() methods, I am assuming you should change the relationship in the User model as:

public $belongsTo = array('Role');

Also make sure that $this->request->data['User'] contains the role_id field correctly when calling $this->User->save($this->request->data) in the add action.

And a minor change: Use lowercase 'c' in $this->User->Create(); :

$this->User->create();