簡體   English   中英

如何顯示基於會話查詢的用戶消息收件箱?

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

我正在嘗試在Yii應用程序上顯示用戶收件箱,但我寫的標准不正確。

會話基於(user_id,收件人_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);

輸出量

顯示所有郵件收件人

您必須按消息本身進行分組。 但是,如果這是一個較大的文本/字符串字段,則這將不是對數據庫的有效查詢,並且會非常慢。 我敦促您重新審視您的數據庫結構。

我已經實現了非常相似的東西,但是我已經轉換了表格以顯示消息之間的關系。

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)

如果尚未在數據庫表中添加更多信息,則無法將消息划分為對話

除非您的對話是user_id - recipient_id唯一。

在這種情況下,您可以Group By user_id, recipient_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM