title: CakePHP 3: issue saving hasMany associations
body: <p>I'm having trouble saving and updating hasMany associations. It seems that Cake can't patch the entity correctly somehow.</p>
  
  <p>ProductGroups Table:</p>
  
  <pre><code>$this-&gt;hasMany('ProductIdentities', [
       'foreignKey' =&gt; 'product_group_id',
       'saveStrategy' =&gt; 'replace'
  ]);
  </code></pre>
  
  <p>ProductIdentities Table:</p>
  
  <pre><code>$this-&gt;belongsTo('ProductGroups', [
      'foreignKey' =&gt; 'product_group_id'
  ]);
  </code></pre>
  
  <p>ProductGroupsController:</p>
  
  <pre><code>// patch entity
  $productGroup = $this-&gt;ProductGroups-&gt;patchEntity($productGroup, $this-&gt;request-&gt;data, [
      'associated' =&gt; [
          'StaffMembers' =&gt; ['onlyIds' =&gt; true],
          'ProductIdentities' =&gt; ['onlyIds' =&gt; true]
      ]
  ]);
  </code></pre>
  
  <p>$this->request->data:</p>
  
  <pre><code>[
      'group_name' =&gt; 'Creative and Performing Arts',
      'active' =&gt; '1',
      'product_type_id' =&gt; '2',
      'staff_members' =&gt; [
          '_ids' =&gt; [
              (int) 0 =&gt; '103',
              (int) 1 =&gt; '11',
              (int) 2 =&gt; '3'
          ]
      ],
      'product_identities' =&gt; [
          '_ids' =&gt; [
              (int) 0 =&gt; '1760',
              (int) 1 =&gt; '1762',
              (int) 2 =&gt; '1763',
              (int) 3 =&gt; '1764'
          ]
      ]
  ]
  </code></pre>
  
  <p>edit.ctp</p>
  
  <pre><code>&lt;?php
      echo $this-&gt;Form-&gt;input('group_name', 
          ['class' =&gt; 'form-control']);
      echo $this-&gt;Form-&gt;input('active', 
          ['class' =&gt; 'form-control']);
      echo $this-&gt;Form-&gt;input('product_type_id', 
          ['class' =&gt; 'form-control']);
      echo $this-&gt;Form-&gt;input('staff_members._ids', 
          ['options' =&gt; $staffMembers, 
           'class' =&gt; 'form-control height-200']);
      echo $this-&gt;Form-&gt;input('product_identities._ids', 
                  ['options' =&gt; $productIdentities, 
                   'class' =&gt; 'form-control height-500']);
  ?&gt;
  </code></pre>
  
  <p>If a productGroup is not associated with any productIdentities, it saves fine. However if you want to select more productIdentities or unselect some existing ones, the data won't be saved. Instead, Cake will create new records in product_identites table.</p>
  
  <p>After doing a debug, I could see that Cake did not patch entity correctly. See below:</p>
  
  <pre><code>'product_identities' =&gt; [
          (int) 0 =&gt; object(App\Model\Entity\ProductIdentity) {
  
              (int) 0 =&gt; '1760',
              (int) 1 =&gt; '1762',
              (int) 2 =&gt; '1763',
              (int) 3 =&gt; '1764',
              '[new]' =&gt; true,
              '[accessible]' =&gt; [
                  '*' =&gt; true
              ],
              '[dirty]' =&gt; [
                  (int) 0 =&gt; true,
                  (int) 1 =&gt; true,
                  (int) 2 =&gt; true,
                  (int) 3 =&gt; true
              ],
              '[original]' =&gt; [],
              '[virtual]' =&gt; [],
              '[errors]' =&gt; [],
              '[invalid]' =&gt; [],
              '[repository]' =&gt; 'ProductIdentities'
  
          }
      ],
  </code></pre>
  
  <p>The onlyIds option is supposed to stop Cake from creating new records but obviously it didn't work.</p>
  
  <p>And I've formatted my data into the format suggested in Cake's documentation so I'm very confused.</p>
  
  <pre><code>$data = [
      'title' =&gt; 'My new article',
      'body' =&gt; 'The text',
      'user_id' =&gt; 1,
      'comments' =&gt; [
          '_ids' =&gt; [1, 2, 3, 4]
      ]
  ];
  </code></pre>
  
  <p><a href="http://book.cakephp.org/3.0/en/orm/saving-data.html#converting-hasmany-data" rel="nofollow">http://book.cakephp.org/3.0/en/orm/saving-data.html#converting-hasmany-data</a></p>
  
  <p>Any comments/help is appreciated.</p>