Created
November 7, 2021 15:18
-
-
Save gerrymcdonnell/79f502522b744fae0aece2945612439f to your computer and use it in GitHub Desktop.
cakephp3 index method pagnate by users role
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function index(){ | |
$user_id=$this->Auth->user('id'); | |
$role=$this->Auth->user('role'); | |
//check users role and show all questions if admin or just their questions of normal user | |
if($role=='admin'){ | |
//show all questions | |
$this->paginate = [ | |
'limit' =>20, | |
'order' => ['Questions.modified' => 'desc'], | |
'contain' => ['Users', 'Questionscategories'] | |
]; | |
} | |
elseif($role=='user'){ | |
//show only the users stuff | |
//set the pagnate options | |
$this->paginate = [ | |
'limit' =>10, | |
'order' => ['Questions.modified' => 'desc'], | |
'contain' => ['Users', 'Questionscategories'], | |
'conditions'=>['questions.user_id'=>$user_id] | |
]; | |
} | |
$questions = $this->paginate($this->Questions); | |
$this->set(compact('questions')); | |
$this->set('_serialize', ['questions']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment