Facebook like conversations list in CakePHP 2.1 (Related to group-wise maximum, sub-query, join)
-
08-06-2021 - |
سؤال
I have the following MySQL tables:
CREATE TABLE IF NOT EXISTS `conversations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user1_id` int(11) NOT NULL,
`user2_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user1_id_2` (`user1_id`,`user2_id`)
);
CREATE TABLE IF NOT EXISTS `messages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`conversation_id` int(11) NOT NULL,
`sender_id` int(11) NOT NULL,
`recipient_id` int(11) NOT NULL,
`subject` varchar(64) NOT NULL,
`body` text NOT NULL,
`created` datetime DEFAULT NULL,
`is_deleted_by_sender` tinyint(3) unsigned NOT NULL DEFAULT '0',
`is_deleted_by_recipient` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
Note: In conversations table user1_id is less or equal to user2_id.
I want to get a list of conversations of a user X, where each conversation is displayed by the last message that has not been deleted by user X in that conversation, and paginate the result. (just like facebook messages. no matter the last message has been sent by X or the other user, it is shown as the display message.
I came up to the group-wise maximum solution which helped me create the following sub-query:
SELECT MAX(SubTable.id) AS id, conversation_id
FROM newtravel.messages AS SubTable
WHERE (
((SubTable.sender_id = 9) AND (SubTable.is_deleted_by_sender = 0))
OR
((SubTable.recipient_id = 9) AND (SubTable.is_deleted_by_recipient = 0))
)
GROUP BY conversation_id
Can we use this sub-query as a join table in $this->Paginator->settings array? If the answer is yes, it should generate a query like the following:
SELECT m1.id, m1.conversation_id, m1.body, m1.sender_id, m1.recipient_id
FROM messages m1
INNER JOIN ( THE_SUB_QUERY_ABOVE ) m2
ON ( m1.conversation_id = m2.conversation_id
AND m1.id = m2.id )
ORDER BY m1.id DESC
This final query returns the wanted results. But I couldn't figure out a way to set the right options in the PaginatorComponent. The official documentation is insufficient for this kind of query. So, how can we configure the find conditions, joins, sub-queries, etc. in this situation?
المحلول
Alright. I've solved this problem as follows:
$this->Message->Behaviors->attach('Containable');
$conditionsSubQuery = array(
'OR' => array(
array(
'SubTable.sender_id' => $this->Auth->user('id'),
'SubTable.is_deleted_by_sender' => 0
),
array(
'SubTable.recipient_id' => $this->Auth->user('id'),
'SubTable.is_deleted_by_recipient' => 0
)
)
);
$db = $this->Message->getDataSource();
$subQuery = $db->buildStatement(
array(
'fields' => array('MAX(id)'),
'table' => $db->fullTableName($this->Message),
'alias' => 'SubTable',
'limit' => null,
'offset' => null,
'joins' => array(),
'conditions' => $conditionsSubQuery,
'order' => null,
'group' => 'conversation_id',
), $this->Message
);
$subQuery = ' Message.id IN (' . $subQuery . ') ';
$this->Paginator->settings = array(
'Message' => array(
'limit' => 5,
'maxLimit' => 5,
'contain' => array(
'Sender' => array(
'id', 'name', 'is_online', 'profile_image', 'thumbnail'
),
'Recipient' => array(
'id', 'name', 'is_online', 'profile_image', 'thumbnail'
),
'Conversation'
),
'order' => 'Message.id DESC'
)
);
$this->set('conversations', $this->Paginator->paginate('Message', $subQuery));
Note that "Message" belongsTo "Sender" and "Recipient", which are aliases for "users" table.