如何在cakephp中获取用户的所有其他用户的最后消息

时间:2014-03-11 07:12:59

标签: mysql cakephp

enter image description here

$options = array('order' => array('MemberMessage.send_date_time' => 'desc'),
  'fields' => array('MemberMessage.id','MemberMessage.message'),
  'group' => 'MemberMessage.message_reciever_id');

 $this->MemberMessage->recursive = 0;
 $MemberMessages = $this->MemberMessage->find('all',$options);

2 个答案:

答案 0 :(得分:0)

我认为这应该足够了吗?

$lastMessage = $this->Message->find('first', array(
    'order' => array('Message.created' => 'desc')
));
祝你好运!

答案 1 :(得分:0)

我通过使用此查询来解决此问题:

$ MemberMessages = $ this-> MemberMessage-> query(" SELECT *                                                         FROM(

                                                    SELECT conversation_user_id,message, send_date_time
                                                    FROM (

                                                    SELECT DISTINCT message_reciever_id AS conversation_user_id, id, message, send_date_time
                                                    FROM (

                                                    SELECT *
                                                    FROM member_messages
                                                    WHERE message_sender_id =".$this->request->data['MemberMessage']['user_id'].
                                                    " ORDER BY send_date_time DESC
                                                    ) AS t
                                                    GROUP BY message_reciever_id
                                                    UNION
                                                    SELECT DISTINCT message_sender_id AS conversation_user_id, id, message, send_date_time
                                                    FROM (

                                                    SELECT *
                                                    FROM member_messages
                                                    WHERE message_reciever_id =".$this->request->data['MemberMessage']['user_id'].
                                                    " ORDER BY send_date_time DESC
                                                    ) AS u
                                                    GROUP BY message_sender_id
                                                    ) AS CONVERSATION
                                                    ORDER BY send_date_time DESC
                                                    ) AS conversation
                                                    GROUP BY conversation_user_id");