n2oforthesoul
n2oforthesoul

Reputation: 1

CakePHP User Role Retrieval

Im trying to pass a condition to my UserController in order to view certain Users whose Role matches to the page I'm trying to access.

For example:

/admin => User.admin

/student => User.student

I'm able to retrieve a list of all registered students using the find('all') function, but when I try to filter roles through setting conditions I'm getting an error that references my code and the role I'm trying to retrieve.

Here's my function in my controller:

    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());
    }

Any tips?

Upvotes: 0

Views: 78

Answers (2)

Ataboy Josef
Ataboy Josef

Reputation: 2101

For adding conditions, use WHERE option with find().

Like:

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());
}

OR

If role is the DB table field name, the code is simple:

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

Upvotes: 0

skrilled
skrilled

Reputation: 5371

conditions should be an array of conditions.

i.e. array('role' => 'role_value')

See documentation at http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

Upvotes: 0

Related Questions