Cakephp3- paginator table sorting not working


#1

Version: 3.3.*

I’m using the $paginator->sort() method to create links in the column headers for tables of paginated search results in my CMS. You should be able to click them once to sort in ascending order, and then click again to reverse into descending order. But the reverse was never working for me.

But this is not for all fields. Let say I have 5 fields.

<tr class="design">
    <th scope="col"><?= $this->Paginator->sort('Students.id','Id',array('class'=>'link')); ?></th>
    <th scope="col"><?php echo $this->Paginator->sort('Students.userName','Name',array('class'=>'link')); ?></th>
    <th scope="col"><?php echo $this->Paginator->sort('Students.age','Age',array('class'=>'link')); ?></th>
    <th scope="col"><?php echo $this->Paginator->sort('Students.currentClass','Class',array('class'=>'link')); ?></th>
    <th scope="col"><?php echo $this->Paginator->sort('Students.dateJoined','Joined Date',array('class'=>'link')); ?></th>
</tr>

I can sort the table both ways using username, age, and school but not using id and joined. When I fetch the list initially I have specified in my Model function to fetch result based on id and joined in ascending order. Is it because of this, I’m not being able to sort it in descending order?

Is there any way I can achieve this?

Controller function

public function index()
{
    //Listing Students
    $this->paginate = [
                        'finder'        =>  'ListStudents',
                        'sortWhitelist' => ['Students.id',
                                            'Students.userName',
                                            'Students.age',
                                            'Students.currentClass',
                                            'Students.dateJoined',],
                        'limit'         => 25,
                        ];
    $students = $this->paginate($this->Students);  
    $this->set(compact('students'));
    $this->set('_serialize', ['students']);
}

Model Function

public function findListStudents(Query $query, array $options)
{   
    $query 
            ->select(['id','userName','age','currentClass','dateJoined'
                ])
    $query->group('Students.id');

    $direction  = (!empty($options['extraOptions']['params']['direction'])) ? $options['extraOptions']['params']['direction'] : 'asc';
    $query->order(['status' => 'asc']);
    $query->order(['Students.dateJoined' => $direction]);
    $query->order(['Students.id' => 'asc']);  
    return $query;
}