rss

Jan
19

CakePHP – Pagination Sorting on 2 Columns

Posted (Amit) in PHP on 19-01-2010

I was just wondering how one can sort on two columns at the same time through CakePHP framework. It was a need for one om project and i was using whole lot of other components and helpers along with pagination, so i found a way out for doing this.

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


$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


  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. $this->passedArgs = array( ‘order’ => $sortParams, ‘keywords’ => $this->passedArgs['keywords']);
  12. }
Share

Post a comment



Amit Yadav is Stephen Fry proof thanks to caching by WP Super Cache