php - 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.
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 ?!
and what about if I used yii relations ?!
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
Answer
Solution:
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.
Using this I can search top level conversations
Using this scheme, for a new message, the reply_to field will be 0.
In Yii
When viewing a message and creating a reply, set the reply_to code to the value of the upper level. This allows infinite number of nesting.
Answer
Solution:
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.In that case you can
Group By user_id, recipient_id