CakePHP按一列排序,然后按另一列排序

时间:2014-02-17 20:33:56

标签: php sorting cakephp pagination

CakePHP中有没有按多列排序分页? 我有这个代码

$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'));

它将产生类似于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  |

无论如何,它可以按状态排序,然后按截止日期排序,因此它看起来像这样

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  |

并且可能在按任务名称排序之后......

谢谢

3 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:0)

如果您使用html表来呈现结果。然后,为了实现所述功能,一种可能的解决方案是在列标题的点击上发送ajax请求。在控制器中,您可以将先前请求的已排序列保存到ajax中。在下一个排序请求期间,检查是否收到任何其他先前的排序请求。

如果您可以使用jquery插件,this link将帮助您对多列进行排序。

答案 2 :(得分:0)

轻松插入'paramType'=> 'querystring',显示代码示例:

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

),
'paramType' => 'querystring',

);

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

Saludos:​​ - )