title: Multiple relations to the same model CakePHP
body: <p>Hey we have three tables in our database which are connected through two relationships which are Account and Invoices.</p>
  
  <p>Accounts (id....)
  Invoices (id, sender_id, receiver_id)
  Relationships (id, sender_id, receiver_id)</p>
  
  <p>Sender and receiver are both foreign keys which reference the account table so in cakePHP an account_id. The relationship table specifies relationships where invoices can be sent or received and the invoice table displays the invoices that have been sent.</p>
  
  <p>How do we link both these foreign keys with Accounts in CakePHP? </p>
  
  <p>Cake is finding there is a relationship and listing the receivers available to send an invoice to. At the moment its sending the right sender_id to the database but the sending the relationship_id to the database as the receiver_id in the invoice table.</p>
  
  <p>Already gone through this but doesnt work: <a href="http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm" rel="nofollow">http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm</a></p>
  
  <p>Here is what we have for our two models:</p>
  
  <p>Account model:</p>
  
  <pre><code>class Account extends AppModel{
  
      var $name='Account';
      public $useTable = 'accounts';
      public $primaryKey = 'id';
      var $hasAndBelongsToMany = array(
          'User' =&gt;
              array(
                  'className'=&gt;'User',
                  )
              );
      var $hasMany = array(
          'Template' =&gt;
              array(
                  'className'=&gt;'Template',
                  'foreignKey'=&gt;'account_id',     
              ),  
              'InvoiceRecieved' =&gt; array(
                  'className'=&gt;'Invoice',
                  'foreignKey'=&gt;'receiver_id',        
              ),
              'InvoiceSent' =&gt; array(
                  'className'=&gt;'Invoice',
                  'foreignKey'=&gt;'sender_id',      
              )
  
          );
  
  
  
      }
  </code></pre>
  
  <p>Invoice model:</p>
  
  <pre><code>class Invoice extends AppModel{ 
      var $name='Invoice'; 
      var $hasAndBelongsToMany = array(
          'Field' =&gt;
              array(
                  'className'=&gt;'Field',
                  'joinTable'=&gt;'fields_invoices'
  
                  )
              );
      var $belongsTo = array(
              'Sender' =&gt; array(
              'className' =&gt; 'Account',
              'foreignKey' =&gt;'account_id',
              ),
              'Receiver' =&gt; array(
              'className' =&gt; 'Account',
              'foreignKey' =&gt;'receiver_id',
              )
          ); 
  
      public $useTable='invoices';
      public $primaryKey='id';
  </code></pre>
  
  <p>Invoice Controller:    </p>
  
  <pre><code> $accounts2=$this-&gt;User-&gt;AccountsUser-&gt;find('list', array(
              'fields'=&gt;array('account_id'),'conditions' =&gt; array(
              'user_id' =&gt; $this-&gt;Auth-&gt;user('id'))));
  
      $accounts=$this-&gt;User-&gt;Relationship-&gt;find('list', array('fields'=&gt;array('receiver_id'),'conditions' =&gt;  array('sender_id' =&gt; $accounts2)));
  
  if($this-&gt;request-&gt;is('post')){
          ($this-&gt;Invoice-&gt;set($this-&gt;request-&gt;data));
          //if($this-&gt;Invoice-&gt;validates(array('fieldList'=&gt;array('receiver_id','Invoice.relationshipExists')))){
  
              $this-&gt;Invoice-&gt;save($this-&gt;request-&gt;data); 
              $this-&gt;Session-&gt;setFlash('The invoice has been saved');  
        }else { 
                  $this-&gt;Session-&gt;setFlash('The invoice could not be saved. Please, try again.');
               }
      //}
       $this-&gt;set('accounts', $accounts); 
       $this-&gt;set('accounts2', $accounts2); 
  </code></pre>
  
  <p>Add view:</p>
  
  <pre><code>&lt;?php
  echo $this-&gt;Form-&gt;create('Invoice', array('action'=&gt;'addinvoice'));
  echo $this-&gt;Form-&gt;input('sender_id',array('label'=&gt;'Sender: ', 'type' =&gt; 'select', 'options' =&gt; $accounts3));
  echo $this-&gt;Form-&gt;input('receiver_id',array('label'=&gt;'Receiver: ', 'type' =&gt; 'select', 'options' =&gt; $accounts));
  echo $this-&gt;Form-&gt;end('Click here to submit Invoice');
  
  ?&gt;
  </code></pre>