创建用户时出现Acl错误

创建用户时出现Acl错误

本文介绍了创建用户时出现Acl错误(ARO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存用户,但保存时出现以下错误

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

我在顶部也遇到此错误

我不知道该怎么做,因为我将角色轻松地添加到了aros中时,为什么现在是否给我带来了问题

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

我遵循了

简单,但似乎并非如此简单。

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

角色模型中

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.'));
            }
        }
    }


推荐答案

我同意吉米的观点,您应该将关系更改为 $ belongsTo 。当您保存用户时,它也会尝试保存aros表,因此,如果您不想将用户保存在aros中,则只需要角色就足以保存aros表,因此您需要修改一些更改(本教程中也对此进行了说明) ,因此请查看我在这两个模型中所做的更改。

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.

角色模型应为:

...
    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函数的更改。

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.

希望它帮助

这篇关于创建用户时出现Acl错误(ARO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 14:42