I am trying to implement a Message System (user to user) into my cakephp framework.
Therefore i created the following tables:
Messages
- id
- title
- body
- sender_id
- created
- response_on
Messages_Users
- id
- message_id
- 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.
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 ishasAndBelongsToMany. Furthermore the associations you’ve put together in theMessage Modelare 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 thehasManytoMessageUser, thebelongsToUserand the wronghasAndBelongsToMessageand add ahasAndBelongsToManyassociation to User.If you want to have
Message hasAndBelongsToMany Userjust 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
hasManyso there is the possibility to use the so-calledhasManyThrough 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.