问题
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.'));
}
}
}
回答1:
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
回答2:
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();
来源:https://stackoverflow.com/questions/22081010/acl-error-aro-when-creating-user