title: Cakephp 3 delete filtering by associated data
body: <p>In CakePHP 3, suppose I want to delete all <strong>Departments</strong> that have some <strong>Employee</strong> named "John" (<em>Departments have many Employees, Employees belongs to Department</em>) </p>
  
  <p>The easiest approach is to filter all those Departments, and delete them one by one in a foreach loop:</p>
  
  <pre><code>$departments = $this-&gt;Departments-&gt;find()
      -&gt;matching('Employees', function ($q) {
          return $q-&gt;where(['Employees.name' =&gt; 'John']);
      })
      -&gt;all();
  
  foreach ($departments as $department) {
      $this-&gt;Departments-&gt;delete($department);
  }
  </code></pre>
  
  <p>This will result in one SELECT query plus one DELETE query for each record. I'd prefer one only query to be sent and executed by the database. Something like this:</p>
  
  <pre><code>DELETE Departments 
  FROM departments Departments 
  INNER JOIN employees Employees  ON 
      Employees.department_id = Departments.id 
      AND Employees.name = 'John';
  </code></pre>
  
  <p>Reading the docs I find three ways to delete records:</p>
  
  <ul>
  <li><a href="http://book.cakephp.org/3.0/en/orm/deleting-data.html" rel="nofollow">Using the Table object one by one</a>, get the object, then delete it.</li>
  <li><a href="http://book.cakephp.org/3.0/en/orm/deleting-data.html#bulk-deletes" rel="nofollow">Using the Table with <strong>deleteAll</strong></a>, which takes an array of conditions over the target table, not the associated ones.</li>
  <li><a href="http://book.cakephp.org/3.0/en/orm/database-basics.html#running-delete-statements" rel="nofollow">Using the ORM</a>, again with only an array of conditions.</li>
  </ul>
  
  <p>Is there an elegant way to obtain my query using CakePHP 3 and the ORM?</p>