我试图保存用户,但当我保存时,我得到以下错误
AclNode:: node () -找不到由"Array ( Aro0.model => Aro0.foreign_key => )“标识的Aro节点
在顶部我也会看到这个错误
未定义索引: role_id CORE\Cake\Model\AclNode.php,第140行
我不知道该怎么做,因为当我增加角色时,它轻松地把它们添加到aros中,那么为什么它现在给我带来了问题?
我遵循了
http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html
这应该是简单的,但它似乎没有那么简单。
在角色模型中
public $primaryKey = 'role_id';
public $displayField = 'role';
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
return null;
}在用户模型中
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));
}
}用于用户的保存代码
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.'));
}
}
}发布于 2014-02-28 08:34:08
我同意吉米的观点,你应该改变和$belongsTo的关系。当您保存用户时,它也会尝试保存aros表,如果您不希望将用户保存在aros中,则需要修改一些更改,只有角色才足以保存aros表(这在教程中也有解释),所以请参阅我在这两个模型中所做的更改。
应以以下方式为榜样:
...
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'role_id',
)
);
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
return null;
}
...用户模型应该是:
...
public $belongsTo = array(
'Role' => array(
'className' => 'Role',
'foreignKey' => 'role_id',
)
);
public function bindNode($user) {
return array('model' => 'Role', 'foreign_key' => $user['User']['role_id']);
}
...如上面的代码所示,public $actsAs = array('Acl' => array('type' => 'requester'));应该只在角色模型上,查看我添加的关系,还可以看到parentNode和bindNode函数的更改。
试试这个,如果有什么事就联系我。
希望它能帮上忙
发布于 2014-02-28 01:33:33
根据您的parentNode()方法,我假设您应该将用户模型中的关系更改为:
public $belongsTo = array('Role');在add操作中调用$this->User-> role_id ($this->request->data)时,还要确保$this->request‘User’正确地包含此字段。
还有一个小改动:在$this->User->Create();中使用小写的'c‘:
$this->User->create();https://stackoverflow.com/questions/22081010
复制相似问题