Thursday, October 18, 2012

Re: Cakephp find order by then group by

First of of try to do each and everything using arrays wherever possible. It will maintain the spirit of CakePHP. Use following code :

$this->Message->find('all',
    array(
        'conditions' => array('Message.receiver_id'=>$user),
        'order' => array('Message.created' => 'Desc'),
        'group' => array('Message.user_id'),
        'field' => array('Message.user_id',...........)
    )
)

We use group by only if we have to perform some group function like sum, count etc...and fields option is compulsory.




On Thursday, 18 October 2012 15:17:53 UTC+5:30, anwar korti wrote:

Hello In a Message controller I try to display the latest message of each user (sender) for a connected one (receiver)  I try this but I still find the first entry. Thank you for your help

Messages table:

  • id
  • user_id -> Sender
  • receiver_id -> receiver
  • content 
  • created -> datetime
  • statut -> read or not

$this->set('messages', $this->Message->find('all',
array
(
'conditions' => array('Message.receiver_id'=>$user),
'order' => array('Message.created desc'),
'group'=>'Message.user_id')));

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: