title: Cakephp3 save many to many table example tags
body: <p>I am trying to create a multi select drop down in Cakephp3. Then update a many to many </p>
  
  <p>I have a post table and tags table and a post_tags table the models have the following foreign keys</p>
  
  <pre><code>PostsTable
  $this-&gt;hasMany('PostTags', [
       'foreignKey' =&gt; 'post_id'
  ]);
  
  
  TagsTable
  $this-&gt;hasMany('PostTags', [
       'foreignKey' =&gt; 'tag_id'
  ]);
  
  PostTagsTable
  $this-&gt;belongsTo('Tags', [
      'foreignKey' =&gt; 'tag_id',
       'joinType' =&gt; 'INNER'
  ]);
  </code></pre>
  
  <p>the post tags table has a post_id and tag_id </p>
  
  <p>in my post add view I have</p>
  
  <p>echo $this->Form->input('tags', ['class'=>'form-control', 'id'=>'select4', 'label' => false, 'options' => $tags, 'multiple' => 'multiple']);</p>
  
  <p>When I debug in the controller I get</p>
  
  <pre><code>'tags' =&gt; [
      (int) 0 =&gt; '1',
      (int) 1 =&gt; '2'
  ],
  </code></pre>
  
  <p>When I look on stack overflow it seems I need to have this:
  ( I am not 100% sure this is correct)
  A: </p>
  
  <pre><code>$data = [
      'tags' =&gt; [
          ['id' =&gt; 1],
      ['id' =&gt; 2]
      ]
  ]
  </code></pre>
  
  <p>I tried looping through the $tags  like but the debug results are pretty much the same:
  Looping like this</p>
  
  <pre><code>&lt;div class="input select"&gt;
  &lt;input type="hidden" name="tags"  value=""id="select4_"&gt;
  &lt;select name="tags[]" multiple="multiple" class="select4" size="7"&gt;
  &lt;?php
  echo '&lt;option id="tag1" value="tag1"&gt;option 1&lt;/option&gt;'; 
  echo '&lt;option id="tag2" value="tag2" &gt;option 2&lt;/option&gt;';
  ?&gt;
  &lt;/select&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I will get </p>
  
  <pre><code>'tags' =&gt; [
      (int) 0 =&gt; 'tag1',
      (int) 1 =&gt; 'tag2'
  ],
  </code></pre>
  
  <p>Apparently I need to pass the values as shown in A: to the controller and then do </p>
  
  <pre><code>$post = $this-&gt;Posts-&gt;patchEntity($post, $this-&gt;request-&gt;data, ['associated' =&gt; ['Tags']]);
  </code></pre>
  
  <p>To make it all work. I would appreciate some help on this as I have been struggeling for days with this.
  Thanks</p>