I had been working on a project and stuck in a problem where a view needs two pagination on same model with different conditions in Cakephp 3.
For eg. opened and closed support tickets listing on the same view.
The following is my code. Can someone help me?
//Opened status pagination
$opened_paginate = [
'contain' => ['Comments'],
'conditions' => [
'AND' => ['SupportTickets.status' => '1']
],
'order' => ['SupportTickets.id' => 'DESC'],
'limit' => 1
];
// Closed status pagination
$closed_paginate = [
'contain' => ['Comments'],
'conditions' => [
'AND' => ['SupportTickets.status' => '2']
],
'order' => ['SupportTickets.id' => 'DESC'],
'limit' => 1
];
$this->set('opened', $this->Paginator->paginate(
$this->SupportTickets->find(),
$opened_paginate
));
$this->set('closed', $this->Paginator->paginate(
$this->SupportTickets->find(),
$closed_paginate
));
You can paginate different models in a single action (see the docs), but I don’t think this works out on the same model.
Maybe you could build some weird stuff by using Traits or this and that, but I would recommend to rethink what you are trying to do and eliminate the need for paginating the same model in a single view multiple times.
You can paginate multiple models in a single controller action, using the scope option both in the controller’s $paginate property and in the call to the paginate() method:
// Paginate property
public $paginate = [
'Articles' => ['scope' => 'article'],
'Tags' => ['scope' => 'tag']
];
// In a controller action
$articles = $this->paginate($this->Articles, ['scope' => 'article']);
$tags = $this->paginate($this->Tags, ['scope' => 'tag']);
$this->set(compact('articles', 'tags'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With