如何在cakephp中进行分组和排序

时间:2013-01-28 11:40:03

标签: cakephp group-by

如何在cakephp中按字段分组和按DESC排序?

$this->set('messages', $this->Message->find('all',array('conditions' => array('Message.receiver_id'=>$this->Session->read('Auth.User.id')),'group' => array('Message.user_id'),'order' => array('max(Message.created) ASC'))));

2 个答案:

答案 0 :(得分:1)

在cakePhp中提取记录

$records = $this->Model->find('all', array(
            'conditions' => array(conditons),
            'fields' => array(fieldlist),
            'group' => array('Model.column'),
            'order' => array('Model.column', 'Model.column2 DESC')
        ));

在您的代码中,您可以编写为

$data = $this->Message->find('all', array(
               'conditions' => array( 'Message.receiver_id'=>$this->Session->read('Auth.User.id')),
               'group' => array('Message.user_id'),
               'order' => array('max(Message.created) ASC')
               ));

$this->set('messages', $data);

答案 1 :(得分:0)

$records = $this->Model->find('all', array(
            'conditions' => array(conditons),
            'recursive' => -1,
            'fields' => array(fieldlist),
            'group' => array('Model.column'),
                    'order' => array('Model.column', 'Model.column2 DESC')
        ));

如果您想按插入的最新消息排序,而不是按created排序,请按id排序Model.id DESC'