Thursday, October 18, 2012

Re: Cakephp find order by then group by

You are doing a find all with a desc sort on created, so in theory you're going to get back multiple rows (messages). Don't you want to do a find first with the same order? Then you'd get back the latest message. Also, if it's a single row you expect back it's probably more correct to call the variable $latestMessage, rather than $messages (which suggest plural).

Jeremy Burns
Class Outfit

http://www.classoutfit.com

On 18 Oct 2012, at 10:47:52, anwar korti <anwarkorti@gmail.com> 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: