title: CakePHP 3 - Deleting associated data using ajax
body: <p>Posts can have multiple authors.</p>
  
  <p>I want to add the possibility to delete authors in a post using Ajax.</p>
  
  <p><strong>PostsAuthorsTable.php</strong></p>
  
  <pre><code>$this-&gt;belongsTo('Users', [
      'className' =&gt; 'Users',
      'foreignKey'    =&gt; 'user_id',
      'joinType' =&gt; 'INNER'
  ]);
  </code></pre>
  
  <p><strong>PostsTable.php</strong></p>
  
  <pre><code>$this-&gt;hasMany('PostsAuthors', [ 'foreignKey' =&gt; 'post_id' ]);
  </code></pre>
  
  <p><strong>PostsController.php</strong></p>
  
  <pre><code>public function delete_author($authorID)
  {
      $id = $_GET['authorID'];
      $this-&gt;Posts-&gt;PostsAuthors-&gt;delete($id);
      $this-&gt;autoRender = false;
  }
  </code></pre>
  
  <p><strong>View: Posts > edit.ctp (JavaScript part)</strong></p>
  
  <pre><code>&lt;?php $this-&gt;start('script'); ?&gt;
  &lt;script&gt;
      $(document).ready(function () { 
          $('.delete-author').on('click', function () {
              $.ajax({
                  url: '&lt;?= $this-&gt;Url-&gt;build(["action" =&gt; "delete_author"]) ?&gt;',
                  type: 'GET',
                  data: { authorID: $(this).data('id') }
              });
  
              return false;
          });
      });
  &lt;/script&gt;
  &lt;?php $this-&gt;end(); ?&gt;
  </code></pre>
  
  <p><strong>View: Posts > edit.ctp (HTML part)</strong></p>
  
  <pre><code>&lt;?php foreach ($post_authors as $post_author): ?&gt;
  &lt;tr&gt;
      &lt;td&gt;&lt;?= $post_author-&gt;user-&gt;name ?&gt;&lt;/td&gt;
      &lt;td class="text-center"&gt;
          &lt;a href="#" class="delete-author text-danger" data-id="&lt;?= $post_author-&gt;id ?&gt;"&gt;
              &lt;i class="icon-trash"&gt;&lt;/i&gt;
          &lt;/a&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>Thank you for your help. :)</p>