Harts
Harts

Reputation: 4093

CakePHP sort by one column then another column

Is there anyway in CakePHP to sort pagination by multiple column? I have this code

$this->paginate = array(
    'Project' => array(
        'limit' => $limit,
        'fields' => array(
                    'DISTINCT (Project.id) AS id',
                    'Project.project_type_id',
                    'Project.contact_id',
                    'Project.company_id',
                    'Project.due_date',
                    'Project.subject',
                    'Project.description',
                    'Project.created',
                    'Project.creator',
                    'Project.project_status_id',
                    'Project.modified',
                    'Project.complete_date',
                    ),
        'joins' => array(
            array(
                'table' => 'project_reminder_users',
                'alias' => 'ProjectReminderUser',
                'type' => 'left',
                'conditions' => array(
                    'Project.id=ProjectReminderUser.project_id',
                ),
            ),
        ),              
        'conditions' => array(
            'User.group_id' => $this->Session->read('Auth.User.group_id'),
            'Project.creator' => $this->Session->read('Auth.User.id'),
        ),
        'order' => 'Project.project_status_id',
    )
);

$this->set('groupTasks', $this->paginate('Project'));

and it will result in something like this with order of Project.project_status_id

Task Name   |   Due Date    |   Status  |
Task 1      |   -           |   Open    |
Task 2      |   2/15/2014   |   Open    |
Task 3      |   2/28/2014   |   Open    |
Task 4      |   2/20/2014   |   Open    |
Task 5      |   -           |   Open    |
Task 6      |   -           |   Open    |
Task 7      |   -           |   Closed  |
Task 8      |   -           |   Closed  |

Is there anyway so it can sort by Status then by due date so it will look like this

Task Name   |   Due Date    |   Status  |
Task 1      |   -           |   Open    |
Task 5      |   -           |   Open    |
Task 6      |   -           |   Open    |
Task 2      |   2/15/2014   |   Open    |
Task 4      |   2/20/2014   |   Open    |
Task 3      |   2/28/2014   |   Open    |
Task 7      |   -           |   Closed  |
Task 8      |   -           |   Closed  |

and maybe after that sort by task name..

Thank you

Upvotes: 0

Views: 1615

Answers (3)

Sergio
Sergio

Reputation: 2469

Easy insert 'paramType' => 'querystring', Show Code Example:

$this->paginate = array(
'conditions' => $conditions,
'order' => array(
    'Post.name' => 'ASC',
    'Post.created' => 'DESC',

),
'paramType' => 'querystring',

);

$this->set('posts', $this->paginate('Post'));

Saludos :-)

Upvotes: 0

Arun Jain
Arun Jain

Reputation: 5464

If you are using the html table to render the result. Then to implement the stated functionality one possible solution is to send an ajax request on the click of column header. In the controller, you can save the previous requested sorted column into ajax. And during the next sort request, check if there is any other previously sort request received or not.

If you can use the jquery plugin, this link will help you to sort multiple column.

Upvotes: 0

kicaj
kicaj

Reputation: 2968

Try this:

'order' => array(
    'first_field' => 'DESC',
    'second_field' => 'ASC'
)

Upvotes: 2

Related Questions