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

  • SEARCH
  • Home
  • 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 3978062
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:59:58+00:00 2026-05-20T04:59:58+00:00

Hi, I need to do the following query using the CakePHP find method: SELECT

  • 0

Hi,

I need to do the following query using the CakePHP find method:

SELECT *
FROM `messages`
INNER JOIN users ON messages.from = users.id
WHERE messages.to = 4
ORDER BY messages.datetime DESC

Basically I have:

  • messages table with a Message model
  • users table with User model

and want to retrieve information from both tables in one query. The users.id field is the same as the messages.from field, so that’s what the join is on.

I am doing it in my MessagesController so it would need to be something like:

$this->Message->find();

Thanks

  • 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-05-20T04:59:58+00:00Added an answer on May 20, 2026 at 4:59 am

    There are two main ways that you can do this. One of them is the standard CakePHP way, and the other is using a custom join.

    It’s worth pointing out that this advice is for CakePHP 2.x, not 3.x.

    The CakePHP Way

    You would create a relationship with your User model and Messages Model, and use the containable behavior:

    class User extends AppModel {
        public $actsAs = array('Containable');
        public $hasMany = array('Message');
    }
    
    class Message extends AppModel {
        public $actsAs = array('Containable');
        public $belongsTo = array('User');
    }
    

    You need to change the messages.from column to be messages.user_id so that cake can automagically associate the records for you.

    Then you can do this from the messages controller:

    $this->Message->find('all', array(
        'contain' => array('User')
        'conditions' => array(
            'Message.to' => 4
        ),
        'order' => 'Message.datetime DESC'
    ));
    

    The (other) CakePHP way

    I recommend using the first method, because it will save you a lot of time and work. The first method also does the groundwork of setting up a relationship which can be used for any number of other find calls and conditions besides the one you need now. However, cakePHP does support a syntax for defining your own joins. It would be done like this, from the MessagesController:

    $this->Message->find('all', array(
        'joins' => array(
            array(
                'table' => 'users',
                'alias' => 'UserJoin',
                'type' => 'INNER',
                'conditions' => array(
                    'UserJoin.id = Message.from'
                )
            )
        ),
        'conditions' => array(
            'Message.to' => 4
        ),
        'fields' => array('UserJoin.*', 'Message.*'),
        'order' => 'Message.datetime DESC'
    ));
    

    Note, I’ve left the field name messages.from the same as your current table in this example.

    Using two relationships to the same model

    Here is how you can do the first example using two relationships to the same model:

    class User extends AppModel {
        public $actsAs = array('Containable');
        public $hasMany = array(
            'MessagesSent' => array(
                'className'  => 'Message',
                'foreignKey' => 'from'
             )
        );
        public $belongsTo = array(
            'MessagesReceived' => array(
                'className'  => 'Message',
                'foreignKey' => 'to'
             )
        );
    }
    
    class Message extends AppModel {
        public $actsAs = array('Containable');
        public $belongsTo = array(
            'UserFrom' => array(
                'className'  => 'User',
                'foreignKey' => 'from'
            )
        );
        public $hasMany = array(
            'UserTo' => array(
                'className'  => 'User',
                'foreignKey' => 'to'
            )
        );
    }
    

    Now you can do your find call like this:

    $this->Message->find('all', array(
        'contain' => array('UserFrom')
        'conditions' => array(
            'Message.to' => 4
        ),
        'order' => 'Message.datetime DESC'
    ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute the following query using Subsonic: SELECT MAX([restore_date]) FROM [msdb].[dbo].[restorehistory] While
I need to replicate the following working HQL query using criteria API. session.CreateQuery( select
I have the following Query and i need the query to fetch data from
I got app that need to be recoded in CakePHP. I got following select
as example, I've the following SQL query ( using Access 2007 ) SELECT ID,
I am wondering how to obtain the following from a query using xml on
I need to modify the following MySQL statement to include information from a third
I need to select the position of a following sibling via XSL. This is
The following query isn't efficient and I need to make it run much faster.
I'm working on a project where I need the following. WCF service on the

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.