title: CakePHP saving HABTM data
body: <p>I have 2 models, <code>Client</code> and <code>Security</code>. They are associated with a HATBTM relationship.
  I have made a join table called <code>clients_securities</code>. So a <code>Client</code> can have many securities and a <code>Security</code> can belong to many <code>Client</code>s.</p>
  
  <p><img src="https://i.stack.imgur.com/R0kVb.png" alt="enter image description here"></p>
  
  <p>Here are my relationship definitions:</p>
  
  <pre><code> //Client Model:
  public $hasAndBelongsToMany = array(
      'Security' =&gt;
          array(
              'className' =&gt; 'Security',
              'joinTable' =&gt; 'clients_securities',
              'foreignKey' =&gt; 'client_id',
              'associationForeignKey' =&gt; 'security_id',
              'unique' =&gt; true,
          )
  );
  
   //Security Model:
  public $hasAndBelongsToMany = array(
      'Client' =&gt;
          array(
              'className' =&gt; 'Client',
              'joinTable' =&gt; 'clients_securities',
              'foreignKey' =&gt; 'security_id',
              'associationForeignKey' =&gt; 'client_id',
              'unique' =&gt; true,
          )
  );
  </code></pre>
  
  <p>I then made an edit action in my clients controller and did this:</p>
  
  <pre><code>public function edit($id = null) {
          if (!$id) {
              throw new NotFoundException(__('Invalid client'));
          }
  
          $client = $this-&gt;Client-&gt;findById($id);
          if (!$client) {
              throw new NotFoundException(__('Invalid client'));
          }
  
          if ($this-&gt;request-&gt;is('post') || $this-&gt;request-&gt;is('put')) {
              $this-&gt;Client-&gt;id = $id;
              if ($this-&gt;Client-&gt;saveAll($this-&gt;request-&gt;data)) {
                  $this-&gt;Session-&gt;setFlash(__('Client has been updated.'));
                  return $this-&gt;redirect(array('action' =&gt; 'edit', $id));
              }
              $this-&gt;Session-&gt;setFlash(__('Unable to update client.'));
          }
  
          if (!$this-&gt;request-&gt;data) {
              $this-&gt;request-&gt;data = $client;
              $this-&gt;set('securities', $this-&gt;Client-&gt;Security-&gt;find('list'));
          }
      }
  </code></pre>
  
  <p>In my edit view I build the form using the HtmlHelper, adding the client table fields and generating the multiple select with:</p>
  
  <pre><code>echo $this-&gt;Form-&gt;create('Client'); //start the form for the client model
  echo $this-&gt;Form-&gt;input('Security'); //generates a multi-select popuplated with securities
  </code></pre>
  
  <p>In addition I also have normal direct form inputs for the <code>Client</code> in the same form. e.g.</p>
  
  <pre><code>echo $this-&gt;Form-&gt;input('name'); //Input for the client name
  echo $this-&gt;Form-&gt;input('currency'); //Input for the client currency
  ...
  </code></pre>
  
  <p>All these inputs get generated and populated with the correct values when the form is rendered but only the direct Client data is saved, not the HABTM data from the multi-select.</p>
  
  <p>When I submit the form the <code>clients_securities</code> table is not populated with the join IDs.</p>
  
  <p>What do I need to do to save it correctly and have then have the saved securities be pre-selected when I reload the "edit" view.</p>
  
  <hr>
  
  <p>Edit: To clarify things, here is a <code>pr()</code> of <code>$this-&gt;request-&gt;data</code>. 
  (The values ("ALLCMCT LX Equity") are the correct foreign keys for the <code>securities</code> table):</p>
  
  <pre><code>Array
  (
      [Client] =&gt; Array
          (
              [id] =&gt; 1212
              [name] =&gt; Example Client
              [currency] =&gt; GBP
              [partner] =&gt; 
              [risk_category_id] =&gt; 4
              [client_code_id] =&gt; 1
              [min_cash_balance] =&gt; 0
              [active] =&gt; 1
              [model] =&gt; 0
              [institutional] =&gt; 0
              [non_uk_situs] =&gt; 0
              [reporting_status] =&gt; 0 
          )
  
      [Security] =&gt; Array
          (
              [Security] =&gt; Array
                  (
                      [0] =&gt; .1muslib3 index
                      [1] =&gt; .eurib3 index
                      [2] =&gt; .ukcpi2 index
                  )
  
          )
  
  )
  </code></pre>
  
  <p>So essentially, say my client id was 12345, Cake should insert 2 records in the <code>clients_securities</code> table like so:</p>
  
  <pre><code>id | client_id | security_id
  ----------------------------
  1  | 12345     | ALLCMCT LX Equity
  2  | 12345     | APMKNTD LX Equity
  </code></pre>
  
  <p>If I manually add some join records into <code>clients_securities</code>, when I go to edit a client the securities in the multi-select come up correctly pre-selected showing that the data is being read from the join table. When I save the form it actually removes the join records but does not save new ones.</p>
  
  <p>Additional note: <strong>My security IDs are stored as CHAR(36)</strong> if that has any effect. This is not an auto-increment field, it contains the ticker of the security and each is unique.</p>