title: CakePHP - Saving an hasMany through association
body: <p>I'm currently trying to figure out how to save a hasMany through relationship. </p>
  
  <p>My tables are:</p>
  
  <p>cards:
  <code>id,
  name</code></p>
  
  <p>colors:
  <code>id,
  name,
  color</code></p>
  
  <p>card_colors (associated with the model CardColor):
  <code>id,
  card_id,
  color_id,
  cost</code></p>
  
  <p>hasMany through Association:</p>
  
  <pre><code>Card hasMany CardColor
  Color hasMany CardColor
  CardColor belongsTo Card
  CardColor belongsTo Color
  </code></pre>
  
  <p>In <code>Card::beforeSave()</code>, I'm going to reconstruct my <code>$data</code> variable so that it has this structure:</p>
  
  <pre><code>array(
  'Card' =&gt; array(
      'name' =&gt; 'theCard',
      'CardColor' =&gt; array(
          array(
              'card_id' =&gt; 4,
              'color_id' =&gt; 5,
              'cost' =&gt; 2
          ),
          array(
              'card_id' =&gt; 5,
              'color_id' =&gt; 2,
              'cost' =&gt; 3
          )
      )
  )
  )
  </code></pre>
  
  <p>However, I don't know how to get the card_id for the card that I am currently inserting. Is there a more Cake-y way of saving a hasMany through association (such as getting the card id automatically in some way while saving)?</p>