【问题标题】:CakePHP User Role RetrievalCakePHP 用户角色检索
【发布时间】:2015-07-21 22:13:26
【问题描述】:

我试图将条件传递给我的 UserController 以查看角色与我尝试访问的页面匹配的某些用户。

例如:

/admin => 用户.admin

/student => 用户.student

我可以使用 find('all') 函数检索所有注册学生的列表,但是当我尝试通过设置条件过滤角色时,我收到一个错误,它引用了我的代码和我的角色我试图找回。

这是我在控制器中的功能:

    public function index($role="admin") {
        //debug($role);
        $this->User->recursive = 0;
        $this->request->data('users', $this->User->find('all', 
            array('conditions' => 'User.role')));
        $this->set('users', $this->paginate());
    }

有什么建议吗?

【问题讨论】:

  • 试试这个公共函数 index($role="admin") { //debug($role); $this->User->recursive = 0; $this->request->data('users', $this->User->find('all', array('conditions' => array('User.role'=>'admin')))); $this->set('users', $this->paginate()); }

标签: php cakephp


【解决方案1】:

conditions 应该是一个条件数组。

array('role' => 'role_value')

请参阅http://book.cakephp.org/2.0/en/models/retrieving-your-data.html 上的文档

【讨论】:

    【解决方案2】:

    要添加条件,请使用 WHERE 选项和 find()

    喜欢:

    public function index($role="admin") {
        $this->User->recursive = 0;
        $this->request->data('users',
            $this->User->find()->where(['conditions' => 'User.role']));
        $this->set('users', $this->paginate());
    }
    

    如果roleDB表字段名,代码很简单:

    public function index($role="admin") {
        $this->User->recursive = 0;
        $this->request->data('users',
            $this->User->findByRole());
        $this->set('users', $this->paginate());
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      • 2015-03-30
      相关资源
      最近更新 更多