Pagination Sorting on 2 Columns - CakePHP

  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Posts: 480
  • Loc: India

Post 3+ Months Ago

Hi All,

I am using CakePHP as my development framework.

I have a table having fields "firstname" & "lastname". I am using Autopaginator component with the pagination component.

What i need to do is to have a functionality to sort the resultset on keys like

Code: [ Select ]
"...ORDER BY lastname asc, firstname asc"


Has anyone implemented this in their development. I am stuck as i am not sure where to change the things to get this done.

Thanks,
Amit
  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Posts: 480
  • Loc: India

Post 3+ Months Ago

Hey guys,

I found the answer to my query.
The way to do it is to inject the second sort column into the passed argument array before calling the $this->paginate function. So here is what i did.

PHP Code: [ Select ]
$this->mergeSortParameters(array("xyz.firstname"=> ($this->passedArgs['direction'])? $this->passedArgs['direction'] : 'asc'));


Here is the function that needs to be called before calling the paginate method

PHP Code: [ Select ]
//Function to merge all the sorting parameters
function mergeSortParameters($sort_extras){
   $sortParams = array();
   if( isset($this->passedArgs["sort"]) ) {
      // Use merge of sort request and extras
      $sortParams = am( $sort_extras, array($this->passedArgs["sort"] => $this->passedArgs["direction"]));
   } elseif( isset( $this->paginate['order'] ) ) {
      // Use default sort
      $sortParams = $this->paginate['order'];
   }
 
   $this->passedArgs = array( 'order' => $sortParams, 'keywords' => $this->passedArgs['keywords']);
   }
  1. //Function to merge all the sorting parameters
  2. function mergeSortParameters($sort_extras){
  3.    $sortParams = array();
  4.    if( isset($this->passedArgs["sort"]) ) {
  5.       // Use merge of sort request and extras
  6.       $sortParams = am( $sort_extras, array($this->passedArgs["sort"] => $this->passedArgs["direction"]));
  7.    } elseif( isset( $this->paginate['order'] ) ) {
  8.       // Use default sort
  9.       $sortParams = $this->paginate['order'];
  10.    }
  11.  
  12.    $this->passedArgs = array( 'order' => $sortParams, 'keywords' => $this->passedArgs['keywords']);
  13.    }

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 72 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.