Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8014643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:00:09+00:00 2026-06-04T20:00:09+00:00

I have the following MySQL tables: CREATE TABLE IF NOT EXISTS `conversations` ( `id`

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:00:12+00:00Added an answer on June 4, 2026 at 8:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following mysql tables: CREATE TABLE `video` ( `video_id` int(11) unsigned NOT
I have the following table: CREATE TABLE IF NOT EXISTS `notes` ( `id` int(10)
I have the following sql create statement mysql> CREATE TABLE IF NOT EXISTS `erp`.`je_menus`
I have a MySQL table with following setup: CREATE TABLE IF NOT EXISTS `coords`
I have the following table declared in MySQL: CREATE TABLE IF NOT EXISTS `ci_sessions`
Consider the following SQL: CREATE TABLE USER1 ( pkUSER1_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
I have two mysql tables: /* Table users */ CREATE TABLE IF NOT EXISTS
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
I have a mysql table like this (sql): CREATE TABLE IF NOT EXISTS silver_and_pgm
Assume the following in MySQL: CREATE TABLE users ( id integer auto_increment primary key,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.