我正在尝试在Yii应用程序上显示用户收件箱,但我无法写出正确的标准。
会话基于(user_id,recipient_id)...所以没有会话表,我的问题是如何在没有会话表的情况下对列表会话进行排序?!
如果我使用yii关系怎么样?!
表格结构:
id (int)
message (text)
user_id (int)
recipient_id (int)
sent_at (int)
,我的标准是:
$criteria = new CDbCriteria();
$criteria->condition = "recipient_id=:user_id OR user_id=:user_id";
// $criteria->group ='user_id';
// $criteria->select ='*';
// $criteria->distinct = true;
$criteria->order = "sent_at ASC";
$criteria->limit = 5;
$criteria->params = array(':user_id' => Yii::app()->user->id);
$model = UserMessage::model()->findAll($criteria);
输出
显示所有邮件收件人
答案 0 :(得分:3)
您必须按消息本身进行分组。但是,如果这是一个大的文本/字符串字段,这将不是对数据库的有效查询,并且将非常慢。我将敦促您重新审视您的数据库结构。
我已经实现了非常相似的东西,但是我已经转换了我的表来显示消息之间的关系。
id (int)
message (text)
user_id (int)
recipient_id (int)
sent_at (int)
reply_to (int) default 0 ;;; I added this field
使用此功能,我可以搜索顶级对话
SELECT * from user_message where reply_to is NULL or reply_to = 0;
使用此方案,对于新消息,reply_to字段将为0.
在Yii
$criteria = new CDbCriteria();
$criteria->condition = "reply_to is NULL or reply_to = 0";
$criteria->order = "sent_at ASC";
$criteria->limit = 5;
$model = UserMessage::model()->findAll($criteria);
查看邮件并创建回复时,请将reply_to代码设置为上一级的值。这允许无限数量的嵌套。
MSG : I need help with this question (id = 1, reply_to = 0)
MSG : L Re: I need help with this question (id = 2, reply_to = 1)
MSG : L Re: I need help with this question (id = 3, reply_to = 1)
MSG : L Re: I need help with this question (id = 4, reply_to = 3)
MSG : L Re: I need help with this question (id = 5, reply_to = 3)
MSG : L Re: I need help with this question (id = 8, reply_to = 5)
MSG : L Re: I need help with this question (id = 6, reply_to = 1)
MSG : L Re: I need help with this question (id = 7, reply_to = 6)
答案 1 :(得分:2)
如果您尚未在数据库表格中添加更多信息,则无法将消息划分为对话
除非您的对话是user_id - recipient_id
唯一的。
在这种情况下,你可以Group By user_id, recipient_id