PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 25, 2022

[FIXED] Paginate and Sort bug(s)?

 January 25, 2022     cakephp, cakephp-3.2, pagination     No comments   

Issue

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


Solution

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]]); }


Answered By - Naidim
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing