0

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

Naidim
  • 6,918
  • 2
  • 23
  • 20
  • Where is the code you're using to create your links? – Dave Jul 11 '16 at 15:10
  • Default baked code: `
      Paginator->prev('< ' . __('previous')) ?> Paginator->numbers() ?> Paginator->next(__('next') . ' >') ?>

    Paginator->counter() ?>

    `
    – Naidim Jul 11 '16 at 15:12
  • 1
    1. Yes, it should. 2. Possible duplicate of http://stackoverflow.com/questions/31763413/pagination-sort-link-on-a-virtual-field-entity-property-in-cakephp-3-0 – ndm Jul 12 '16 at 00:01
  • 2
    I think this is a [known issue](https://github.com/cakephp/cakephp/issues/7324). – Greg Schmidt Jul 12 '16 at 01:34

1 Answers1

1

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]]); }
Naidim
  • 6,918
  • 2
  • 23
  • 20