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 8941401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:17:29+00:00 2026-06-15T11:17:29+00:00

I am trying to implement a Message System (user to user) into my cakephp

  • 0

I am trying to implement a Message System (user to user) into my cakephp framework.
Therefore i created the following tables:

Messages

  1. id
  2. title
  3. body
  4. sender_id
  5. created
  6. response_on

Messages_Users

  1. id
  2. message_id
  3. recipient_id

The Messages_Users table is used to store the recipient of each sent message.
Then created the corresponding models like this and set up the relationships between the Models.

Model for Message

<?php class Message extends Model{
public $hasMany = array(
        'MessageUser'=>array(
                'className'=>'MessagesUser',
                'foreign Key'=>'message_id')
        );
public $belongsTo = array (
        'User' =>array(
                'className'=>'User',
                'foreignKey'=>'sender_id')
        );
public $hasAndBelongsTo = array(
        'Message'=>array(
                'className'=>'Message',
                'foreignKey'=>'response_on')
        );

}

Model for MessageUser

 <?php
     class MessageUser extends Model{
    public $belongsTo = array (
        'User'=>array(
            'className'=>'User',
            'foreignKey'=>'user_id'),
        array(
        'Message'=>array(
            'className'=>'Message',
            'foreignKey'=>'message_id')
                )
        );

Model for User

class User extends AppModel{
public $hasAndBelongsTo = array(
        'Message'=>array(
                'joinTable' =>'messages_users',
                'className' =>'Message',
                'foreignKey' =>'recipient_id',
                'associationForeignKey' =>'message_id')
        );

Now i want to implement into my MessagesController a function inbox() which shows all messages stored in the database, whom are sent to the corresponding user.
So my approach was putting the function into the MessagesController

public function inbox(){
    $uid = $this->Auth->user('id');
    $messages = $this->Message->find('all', array(
            'conditions' => array(
                    'recipient_id' => $uid)
            )
            );

The function above should perform a join on the table messages and messages_users via the message_id and select the sets of data where the user_id of table messages_users is equal to the recipient_id.

But all I get is an error saying that the column recipient_id was not found in the where clause.

How do I instruct the find method to join these tables properly?
I thought it would be enough to link the models, so the cake magic would take care of the rest.

  • 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-15T11:17:30+00:00Added an answer on June 15, 2026 at 11:17 am

    The quick answer is that your associations are wrong. The long one follows:

    First of all there is no association called hasAndBelongsTo. The association name is hasAndBelongsToMany. Furthermore the associations you’ve put together in the Message Model are a totally wrong or if I may put it this way – you may have not understood them correctly. So the quick fix would be to remove the hasMany to MessageUser, the belongsTo User and the wrong hasAndBelongsTo Message and add a hasAndBelongsToMany association to User.

    If you want to have Message hasAndBelongsToMany User just use that. How is described here. Also HABTM has be discussed many times here so I’m not going to go into detail about it.

    However I would like to point you to another possible set up. Now I am proposing this because I saw you tried to also use hasMany so there is the possibility to use the so-called hasManyThrough the Join Model, but usually that is to be used when you want to store additional data in the join model (MessageUser in your case). Check this question out.

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

Sidebar

Related Questions

I'm trying to implement a simple messaging system in CakePHP 1.3. What I need
Hey, I'm trying to implement a message system as used in social networks. The
I am trying to implement a message system quite similar to facebook . The
I’m trying to implement on a very big and bloating system a framework to
I'm trying to implement a private message system. Let me know if this is
I'm trying to implement this accepted solution for displaying a custom error message: https://stackoverflow.com/a/5229581/141172
I am trying implement a most recent widget into my tumblr post. So far,
Trying to implement a rating system of users and postings. What is the best
Im trying to implement a destructor for the objects of linked-list Iv created. I
Background: I'm trying to implement a messenging system in my app, and I'm writing

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.