Naidim
Naidim

Reputation: 7166

Paginate and Sort bug(s)?

In my controller I have my pagination set to order by 2 fields.

public $paginate = [
  'limit' => 50,
  'order' => ['first_name', 'last_name']
];
$contacts = $this->paginate($this->Contacts);

This works fine on the first page, but since I left out the default direction => 'ASC' the Paginator links don't work at all:

/contacts?page=2&sort=0&direction=first_name

When I add in the direction, it works, but of course only sorts by the first field, messing up the sort order.

/contacts?page=2&sort=Contacts.first_name&direction=ASC
  1. Should the default direction be explicitly required?
  2. Is there a method to maintain both fields for sorting during pagination?

Sorting by virtual fields (e.g. full_name => first_name . ' ' . last_name) doesn't work as it did in 2.x

Upvotes: 0

Views: 463

Answers (1)

Naidim
Naidim

Reputation: 7166

Solved both issues with the following:

Set the default sort order to be the same as the virtual field:

public $paginate = [
  'order' => ['first_name', 'last_name']
];

Then just add the following to the View to prevent the paginator from overriding the default order unless specified by the user:

if (empty($_GET['direction'])) { $this->Paginator->options(['url' => ['direction' => null, 'sort' => null]]); }

Upvotes: 1

Related Questions