2015-07-21 90 views
0

我試圖將一個條件傳遞給我的UserController,以查看某些用戶的角色與我嘗試訪問的頁面相匹配的用戶。CakePHP用戶角色檢索

例如:

/管理=> User.admin

/學生=> User.student

我能夠檢索使用查找所有已註冊的學生名單(」所有')函數,但是當我嘗試通過設置條件來篩選角色時,我收到一個引用我的代碼和我試圖檢索的角色的錯誤。

這裏是我的功能在我的控制器:

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

任何提示嗎?

+0

試試這個公共功能指數($角色=」管理員「){ // debug($ role); $ this-> User-> recursive = 0; ('users',$ this-> user-> find('all', array('conditions'=> array('User.role'=>'admin'))) ); $ this-> set('users',$ this-> paginate()); } –

回答

0

對於添加條件,參見文檔中使用與find()WHERE選項。

像:

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

如果role數據庫表字段名,代碼很簡單:

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