我尝试在facebook上编写一个消息系统。 我得到了桌子;
USERS
id, name
MESSAGES
user_from, user_to, message
我得到了模特:
USER
public $hasMany = array(
'MessageFrom' => array(
'className' => 'Message',
'foreignKey' => 'user_from'
),
'MessageTo' => array(
'className' => 'Message',
'foreignKey' => 'user_to'
)
);
var $hasAndBelongsToMany = array(
'UserMessages' => array(
'className' => 'User',
'joinTable' => 'messages',
'foreignKey' => 'user_from',
'associationForeignKey' => 'user_to'
)
);
MESSAGE
public $belongsTo = array(
'UserFrom' => array(
'className' => 'User',
'foreignKey' => 'user_from'
),
'UserTo' => array(
'className' => 'User',
'foreignKey' => 'user_to'
)
);
一切都运行良好,我的意思是发送和收集用户的消息,并阅读它。 但我想在用户面板中进行聊天列表。
我必须选择仅包含最后一条消息的所有用户才能创建与用户聊天的列表。
我的数组看起来像:
(int) 0 => array(
'Message' => array(
'id' => '12',
'user_from' => '2',
'user_to' => '1',
'message' => 'Hello user 1!',
),
'UserFrom' => array(
'id' => '2',
'username' => 'user2',
),
'UserTo' => array(
'id' => '1',
'username' => 'user1',
)
),
(int) 1 => array(
'Message' => array(
'id' => '12',
'user_from' => '1',
'user_to' => '2',
'message' => 'Oh Hello user 2!',
),
'UserFrom' => array(
'id' => '1',
'username' => 'user1',
),
'UserTo' => array(
'id' => '2',
'username' => 'user2',
)
),
(int) 2 => array(
'Message' => array(
'id' => '12',
'user_from' => '1',
'user_to' => '5',
'message' => 'Message to user 5 from user 1',
),
'UserFrom' => array(
'id' => '1',
'username' => 'user1',
),
'UserTo' => array(
'id' => '5',
'username' => 'user5',
)
),
现在,对于ex im log as user id = 1我想在聊天列表中看到
USER 5(最后一条消息:来自用户1的用户5的消息) USER 2(最后一条消息:哦,你好用户1)
Simpy:我想在此阵列中只向用户发送最后一条消息或来自此用户的用户... 我不需要所有消息的列表...只有一个最后消息来自或来自用户,如在GMAIL框中。 现在在数组中我有所有用户和消息关联......
我创建了一个函数......它可以获取用户的所有信息...... 如何只获得一个id最大的消息?
public function getChats($user_id) {
$this->order = 'Message.date DESC, Message.id DESC';
$data = $this->find('all', array(
'conditions' => array(
'OR' => array(
array('user_to' => $user_id),
array('user_from' => $user_id)
)
)
));
return $data;
}
-
SELECT `Message`.`id`, `Message`.`user_from`, `Message`.`user_to`, `Message`.`message`, `Message`.`date`, `UserFrom`.`id`, `UserFrom`.`username`, `UserFrom`.`email`, `UserFrom`.`password`, `UserFrom`.`name`, `UserFrom`.`surname`, `UserFrom`.`city`, `UserFrom`.`street`, `UserFrom`.`born_date`, `UserFrom`.`role`, `UserTo`.`id`, `UserTo`.`username`, `UserTo`.`email`, `UserTo`.`password`, `UserTo`.`name`, `UserTo`.`surname`, `UserTo`.`city`, `UserTo`.`street`, `UserTo`.`born_date`, `UserTo`.`role` FROM `gametrade_cake`.`messages` AS `Message` LEFT JOIN `gametrade_cake`.`users` AS `UserFrom` ON (`Message`.`user_from` = `UserFrom`.`id`) LEFT JOIN `gametrade_cake`.`users` AS `UserTo` ON (`Message`.`user_to` = `UserTo`.`id`) WHERE ((`user_to` = 1) OR (`user_from` = 1))
GROUP BY UserFrom.id,UserTo.id
ORDER BY `Message`.`id` DESC
此查询选择我的消息,但我不知道为什么不通过message.id DESC订购... 这个命令由mesage.id ASC ...
任何人都可以帮助我?
答案 0 :(得分:0)
您可以在查询邮件时传递一个时间戳,您必须在保存邮件时保存时间戳
public function getChats($user_id,$timestamp) {
$this->order = 'Message.date DESC, Message.id DESC';
$data = $this->find('all', array(
'conditions' => array(
'OR' => array(
array('user_to' => $user_id),
array('user_from' => $user_id)
),
'created >' => $timestamp
)
));
return $data;
}