2012-05-13 7 views
0

Iは、次のMySQLのテーブルを有する:のFacebook(グループごとの最大サブクエリに関連して、参加)

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

注:会話テーブルuser1_idでuser2_idする以下であります。

ユーザXの会話のリストを取得したいと思います。ここで、各会話は、その会話のユーザXによって削除されていない最後のメッセージで表示され、結果に改ページが行われます。

私はgroup-wise maximum solutionまで来て、次のサブクエリを作成するのに役立ちました:

(私はFacebookのメッセージのように。
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 

我々は答えがイエスであれば、それは次のようなクエリを生成する必要があります$ this-> Paginator->設定の配列に参加するテーブルとしてこのサブクエリを使用できますか?A:

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 

この最終的なクエリは、必要な結果を返しますが、私は方法を理解することができませんでしたPaginatorComponentに適切なオプションを設定します。この種類のクエリでは、公式のドキュメントでは不十分です。したがって、この状況で検索条件、結合、サブクエリなどを設定するにはどうすればよいですか?

答えて

0

いいえ。

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

注意をその「ユーザー」テーブルの別名である「メッセージ」belongsToの「送信者」及び「受信者」、次のように私はこの問題を解決してきました。

関連する問題