title: Relationship problem in Cakephp. How to fetch data?
body: <p>I am trying to make a messages functionality similar to the facebook. Just the message thing and not facebook. A brief descriptions does like this.</p>
  
  <p>1) There are a number of users ( user table) 2) One person can send a message to one or more than one person. 3) There can be multiple reply to the same message. 4) If its send to multiple people. All can reply and it is show to all of them.</p>
  
  <p>Tables used</p>
  
  <p>messages table</p>
  
  <pre><code>id
  timestamp
  sender_id
  subject
  message
  due_date
  urgent_flag
  open_flag
  reply_id
  </code></pre>
  
  <p>message_user (table)</p>
  
  <pre><code>id
  timestamp
  message_id
  receiver_id
  read_flag
  </code></pre>
  
  <p>The CakePHP relations are as follows :</p>
  
  <p>Message Model</p>
  
  <pre><code>var $hasMany = array(
          'MessageUser' =&gt; array(
              'className'     =&gt; 'MessageUser',
              'foreignKey'    =&gt; 'message_id',    
                  )
      );  
  var $belongsTo = array (
      'User' =&gt; array (
          'className' =&gt;  'User',
          'foreignKey' =&gt; 'sender_id',
      )
  );
  var $hasAndBelongsTo=array(
      'Message' =&gt; array (
          'className' =&gt; 'Message',
          'foreignKey' =&gt; 'reply_id',
         )
  );
  </code></pre>
  
  <p>MessageUser Model</p>
  
  <pre><code>var $belongsTo = array (
      'User' =&gt; array (
          'className' =&gt;  'User',
          'foreignKey' =&gt; 'receiver_id',
      ),
      'Message' =&gt; array (
          'className' =&gt;  'Message',
          'foreignKey' =&gt; 'message_id'
  
      )
  );
  </code></pre>
  
  <p>Questions :</p>
  
  <p>1) Is my approach correct ? Or the database schema needs to be revised. 2) If yes, How should I fetch the data for inbox ? This is a bit complex as I want to show the conversation for those messages which people has sent me.</p>
  
  <p>For example, user 1 send message to user 2. User 2 adds 2 replies to the same. Then users 1's inbox should show only 1 message. and when I open it. it will show the previous msgs as well.. (this is similar to facebook)</p>
  
  <p>One more problem which I see here is, How to delete messages ? Suppose user 1 deletes a message it should not show anything in his inbox. but user 2 can see the whole conversation which he had.</p>