CakePHP – Pagination Sorting on 2 Columns
Category : PHP
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
- //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']);
- }












