Note that there are some explanatory texts on larger screens.

plurals
  1. POImplement Message System into cakephp 2.x
    primarykey
    data
    text
    <p>I am trying to implement a Message System (user to user) into my <code>cakephp</code> framework. Therefore i created the following tables:</p> <h2>Messages</h2> <ol> <li>id </li> <li>title</li> <li>body</li> <li>sender_id</li> <li>created </li> <li>response_on</li> </ol> <h2>Messages_Users</h2> <ol> <li>id</li> <li>message_id</li> <li>recipient_id</li> </ol> <p>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.</p> <h2>Model for Message</h2> <pre><code>&lt;?php class Message extends Model{ public $hasMany = array( 'MessageUser'=&gt;array( 'className'=&gt;'MessagesUser', 'foreign Key'=&gt;'message_id') ); public $belongsTo = array ( 'User' =&gt;array( 'className'=&gt;'User', 'foreignKey'=&gt;'sender_id') ); public $hasAndBelongsTo = array( 'Message'=&gt;array( 'className'=&gt;'Message', 'foreignKey'=&gt;'response_on') ); </code></pre> <p>}</p> <h2>Model for MessageUser</h2> <pre><code> &lt;?php class MessageUser extends Model{ public $belongsTo = array ( 'User'=&gt;array( 'className'=&gt;'User', 'foreignKey'=&gt;'user_id'), array( 'Message'=&gt;array( 'className'=&gt;'Message', 'foreignKey'=&gt;'message_id') ) ); </code></pre> <h2>Model for User</h2> <pre><code>class User extends AppModel{ public $hasAndBelongsTo = array( 'Message'=&gt;array( 'joinTable' =&gt;'messages_users', 'className' =&gt;'Message', 'foreignKey' =&gt;'recipient_id', 'associationForeignKey' =&gt;'message_id') ); </code></pre> <p>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 </p> <pre><code>public function inbox(){ $uid = $this-&gt;Auth-&gt;user('id'); $messages = $this-&gt;Message-&gt;find('all', array( 'conditions' =&gt; array( 'recipient_id' =&gt; $uid) ) ); </code></pre> <p>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.</p> <p>But all I get is an error saying that the column <code>recipient_id</code> was not found in the where clause. </p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload