title: cakePHP bind hasmany through relationship
body: <p>Ok this is kind of hard to explain but I'll try my best.</p>
  
  <p>I have 3 tables</p>
  
  <pre><code>companies     products     product_availabilities
  ---------     --------     ----------------------
  id            id           id
  name          name         company_id
                             product_id
                             buys (tinyint)
                             sells (tinyint)
  </code></pre>
  
  <p>And their models</p>
  
  <pre><code>class Company extends AppModel
  {
          public $name = 'Company';
  
          public $hasMany = array(
              'ProductAvailability'
          );
  
  
  class Product extends AppModel
  {       
      public $name = 'Product';
  
      public $hasMany = array(
          'ProductAvailability'
      );
  
  
  class ProductAvailability extends AppModel
  {
      public $name = 'ProductAvailability';
  
      public $belongsTo = array(
          'Company',
          'Product'
      );
  }
  </code></pre>
  
  <p>What I want to do is when I create a company, I want to be able to select products that the company buys or sells. I've seen an example of a hasMany through relationship in the book (http://book.cakephp.org/1.3/view/1650/hasMany-through-The-Join-Model) but they are creating the form from the "join table" controller. Is it possible to bind the productAvailability model to my company model to be able to select the products while creating the company? </p>
  
  <p>Edit : Here is how I've done it. I know it is not optimal as there is a lot of looping involved but it works.</p>
  
  <p>Company controller : </p>
  
  <pre><code>    $products = $this-&gt;Company-&gt;ProductAvailability-&gt;Product-&gt;find('list', array('fields' =&gt; array('Product.id', 'Product.label')));
      $this-&gt;set('products', $products);
  
      if($this-&gt;request-&gt;is('post')){             
          if($this-&gt;Company-&gt;save($this-&gt;request-&gt;data)){
              foreach($products as $product)
              {
                  $tmpArray = array(
                      'company_id' =&gt; $this-&gt;Company-&gt;id,
                      'product_id' =&gt; $product['Product']['id']
                  );
  
                  foreach($this-&gt;request-&gt;data('BuyProducts.product_id') as $buyProduct)
                  {
                      if($buyProduct == $product['Product']['id'])
                          $tmpArray['buys'] = 1;
                  }
                  foreach($this-&gt;request-&gt;data('SellProducts.product_id') as $sellProduct)
                  {
                      if($sellProduct == $product['Product']['id'])
                          $tmpArray['sells'] = 1;
                  }
  
                  if(count($tmpArray) &gt; 2)
                  {
                      $this-&gt;Company-&gt;ProductAvailability-&gt;create();
                      $this-&gt;Company-&gt;ProductAvailability-&gt;set($tmpArray);
                      $this-&gt;Company-&gt;ProductAvailability-&gt;save();
                  }  
              }
  
              $this-&gt;Session-&gt;setFlash('Yay', 'success');
              $this-&gt;redirect(array('action' =&gt; 'index'));
          } else {
              $this-&gt;Session-&gt;setFlash('Nay', 'error');
          }
      }
  </code></pre>
  
  <p>Company add form :</p>
  
  <pre><code>&lt;?php echo $this-&gt;Form-&gt;create('Company'); ?&gt;
  
  &lt;?php echo $this-&gt;Form-&gt;input('name', array( 'div' =&gt; 'full-form')); ?&gt;
  
  &lt;?php echo $this-&gt;Form-&gt;input('BuyProducts.product_id', array('multiple' =&gt; 'checkbox', 'options' =&gt; $products, 'div' =&gt; 'full-form', 'label' =&gt; false)); ?&gt;
  
  &lt;?php echo $this-&gt;Form-&gt;input('SellProducts.product_id', array('multiple' =&gt; 'checkbox', 'options' =&gt; $products, 'div' =&gt; 'full-form', 'label' =&gt; false)); ?&gt;
  
  &lt;?php echo $this-&gt;Form-&gt;end(array('label' =&gt; __('Save'), 'div' =&gt; 'center', 'class' =&gt; 'bouton-vert')); ?&gt;
  </code></pre>