Skip to content

Instantly share code, notes, and snippets.

@gerrymcdonnell
Created November 7, 2021 15:18
Show Gist options
  • Save gerrymcdonnell/79f502522b744fae0aece2945612439f to your computer and use it in GitHub Desktop.
Save gerrymcdonnell/79f502522b744fae0aece2945612439f to your computer and use it in GitHub Desktop.
cakephp3 index method pagnate by users role
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