title: CakePHP 3.x - Save many-to-many data
body: <p>I am attempting to patchEntity with a join table, but I am unable to get it to save the associated records, and I think I have the backend code correct, but I am unsure of what the frontend code should look like... </p>
  
  <p>Here is the scenario, I have three tables</p>
  
  <p>Ledgers Table</p>
  
  <pre><code>id int
  title string
  </code></pre>
  
  <p>Tribes Table</p>
  
  <pre><code>id int
  name string
  </code></pre>
  
  <p>LedgersTribes Table</p>
  
  <pre><code>id int
  ledger_id int
  tribe_id int
  </code></pre>
  
  <p>Here is my backend code</p>
  
  <pre><code>public function ledgerSave($id = null)
  {
      $this-&gt;loadModel('Ledgers');
  
      $ledger = $this-&gt;Ledgers-&gt;get($id, [
          'contain' =&gt; ['LedgersTribes']
      ]);
  
      if ($this-&gt;request-&gt;is(['patch', 'post', 'put'])) {
           $ledger = $this-&gt;Ledgers-&gt;patchEntity($ledger, $this-&gt;request-&gt;getData(), [
               'associated' =&gt; ['LedgersTribes']
           ]);
           if ($this-&gt;Ledgers-&gt;save($ledger)) {
               $this-&gt;Flash-&gt;success(__('The ledger has been saved.'));
  
               return $this-&gt;redirect($this-&gt;referer());
           }
           $this-&gt;Flash-&gt;error(__('The ledger could not be saved. Please, try again.'));
      }
      $this-&gt;set(compact('ledger'));
  }
  </code></pre>
  
  <p>Here is the relevant frontend code</p>
  
  <pre><code>&lt;?= $this-&gt;Form-&gt;control('tribe_id[]', ['type' =&gt; 'checkbox', 'label' =&gt; false, 'class' =&gt; 'minimal', 'value' =&gt; $tribe-&gt;id]) ?&gt;
  </code></pre>
  
  <p>My question is, what should the field name be for the tribe_id, the idea is, i have a list of checkboxes and the user checks off a couple of boxes and then those tribe_id's get inserted into the LedgersTribes table with the ledger_id</p>
  
  <p>Any ideas on how I can do this?</p>
  
  <hr>
  
  <p>EDIT: Here is a screenshot of the form</p>
  
  <p><a href="https://i.stack.imgur.com/OgyAo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OgyAo.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>I have reviewed the following links, and none of them answer my question...</p>
  
  <p><a href="https://stackoverflow.com/questions/31662069/cakephp-3-save-associated-model-fail">CakePHP 3: Save Associated Model Fail</a></p>
  
  <p><a href="https://stackoverflow.com/questions/34378456/save-associated-in-cakephp-3-not-working">Save associated in cakephp 3 not working</a></p>
  
  <p><a href="https://stackoverflow.com/questions/32013532/how-to-save-associated-joindata-in-cakephp-3-x">How to save associated joinData in cakephp 3.x</a></p>
  
  <p><a href="https://stackoverflow.com/questions/32938660/cakephp-3-cannot-save-associated-model">CakePHP 3 cannot save associated model</a></p>
  
  <p><a href="https://stackoverflow.com/questions/44438913/cakephp-3-save-associated-belongstomany-jointable">Cakephp 3 - Save associated belongsToMany (joinTable)</a></p>