简体   繁体   English

如何显示基于会话查询的用户消息收件箱?

[英]How to show user messages inbox based on conversation query?

I'm trying to show user inbox on Yii Application but I can't write a right criteria. 我正在尝试在Yii应用程序上显示用户收件箱,但我写的标准不正确。

Conversation is based on (user_id, recipient_id) ... so there is no conversation table, and my problem is how to sort list conversations without conversations table ?! 会话基于(user_id,收件人_id)...,因此没有会​​话表,而我的问题是如何在没有会话表的情况下对列表会话进行排序?

and what about if I used yii relations ?! 如果我使用yii关系呢?!

Table structure: 表结构:

id (int)
message (text)
user_id (int)
recipient_id (int)
sent_at (int)

and my criteria is: 我的标准是:

    $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);

Output 输出量

It's shown all messages recipient 显示所有邮件收件人

You have to group by the message itself. 您必须按消息本身进行分组。 However, if this is a large text/string field, this will not be an efficient query on the database, and will be very slow. 但是,如果这是一个较大的文本/字符串字段,则这将不是对数据库的有效查询,并且会非常慢。 I will urge you to relook at your database structure. 我敦促您重新审视您的数据库结构。

I have implemented something very similar, but I have converted my table to show the relationship between messages. 我已经实现了非常相似的东西,但是我已经转换了表格以显示消息之间的关系。

id (int)
message (text)
user_id (int)
recipient_id (int)
sent_at (int)
reply_to (int) default 0      ;;; I added this field

Using this I can search top level conversations 使用此工具,我可以搜索顶级对话

SELECT * from user_message where reply_to is NULL or reply_to = 0;

Using this scheme, for a new message, the reply_to field will be 0. 使用此方案,一个新的消息,该REPLY_TO领域将是0。

In Yii 在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);

When viewing a message and creating a reply, set the reply_to code to the value of the upper level. 查看消息并创建回复时,将reply_to代码设置为上一级的值。 This allows infinite number of nesting. 这允许无限数量的嵌套。

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)

If you have not added more information in the database table, you can't divide messages into conversations 如果尚未在数据库表中添加更多信息,则无法将消息划分为对话

Unless, your conversations are user_id - recipient_id unique. 除非您的对话是user_id - recipient_id唯一。

In that case you can Group By user_id, recipient_id 在这种情况下,您可以Group By user_id, recipient_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM