Cakephp Version: 5.0.10
Hi,
Please be advised that I have asked this question on SO (cakephp - Paginating with sort and direction - Stack Overflow) but unfortunately I was unable to find a solution. And without a solution I’m not going to be able to upgrade to 5 hence my post here.
History - My Use Case
For the SOC (Seperation Of Concern) principal I separate the initial page load from the paginated page load in all my index methods with the following code:
Tickets Controller - Index Method
if (empty($this->request->getQuery('page'))
&& is_null($this->request->getQuery('sort'))
&& is_null($this->request->getQuery('direction'))) {
// Initial Load
$this->setSort('Tickets.date_created');
$this->setDirection('desc');
$this->setLimit($user->results_per_page->respp_ticket);
} elseif (($this->request->getQuery('sort') !== '')
&& is_string($this->request->getQuery('sort'))
&& !is_numeric($this->request->getQuery('sort'))
&& ($this->request->getQuery('direction') !== '')
&& is_string($this->request->getQuery('direction'))
&& !is_numeric($this->request->getQuery('direction'))) {
// Paginated Load
$this->setPage($this->request->getQuery('page'));
$this->setSort($this->request->getQuery('sort'));
$this->setDirection($this->request->getQuery('direction'));
}
// Component here which returns a query object named $query
// This was how I used to code in the 4 branch - And this is what I need to refactor
$this->set('page', $this->getPage());
$this->set('sort', $this->getSort());
$this->set('direction', $this->getDirection());
$this->set('limit', $this->getLimit());
$settings = $this->paginate = [
'sortableFields' => [
'Tickets.id',
'Tickets.date_created',
'Tickets.priority',
'Tickets.subject'
],
'page' => $this->getPage(),
'sort' => $this->getSort(),
'direction' => $this->getDirection(),
'limit' => $this->getLimit()
];
$tickets = $this->paginate($query, $settings);
$this->set(compact('tickets'));
With the changes in 5 I now code like this:
protected array $paginate = [
'sortableFields' => [
'Tickets.id',
'Tickets.date_created',
'Tickets.priority',
'Tickets.subject'
],
'order' => [
'Tickets.date_created' => 'desc',
],
];
$this->set('page', $this->getPage());
$this->set('sort', $this->getSort());
$this->set('direction', $this->getDirection());
$this->set('limit', $this->getLimit());
$this->paginate = [
'limit' => $this->getLimit()
];
$tickets = $this->paginate($query);
$this->set(compact('tickets'));
My Issue
The column sorts do sort the columns correctly but the page numbers no longer have the sort and direction in the url.
As far as I can tell the only other way to liaise with the paginator in this context is with pagination options in the index view like this:
$this->Paginator->options([
'url' => [
'?' => [
'sort' => $sort,
'direction' => $direction,
'limit' => $limit
]
]
]);
But if I do this I obviously overwrite the url generated by the helper which means:
- The column sorts always sort the same column - The Tickets.date_created column.
- The page numbers do have the sort and direction in the url.
What I’ve Tried
$this->paginate = [
'sort' => $this->getSort(),
'direction' => $this->getDirection(),
'limit' => $this->getLimit()
];
Result
- The column sorts sort correctly on all columns.
- The page numbers have the sort and direction in the url.
But this warning is displayed:
Warning (512) : Passing query options as paginator settings is no longer supported. Use a custom finder through the finder config or pass a SelectQuery instance to paginate(). Extra keys found are: sort, direction.
I understand query options is referring to the sort and direction. And for me using a finder in this case does not make sense.
Question
If passing query options as paginator settings is no longer supported what is the new v5 way.
Thanks Zenzs.