title: CakePHP 3.x - hasMany through association - find
body: <p>Assuming I have exactly the setup as in CookBook here:
  <a href="http://book.cakephp.org/3.0/en/orm/associations.html" rel="noreferrer">http://book.cakephp.org/3.0/en/orm/associations.html</a></p>
  
  <pre><code>class StudentsTable extends Table
  {
      public function initialize(array $config)
      {
          $this-&gt;belongsToMany('Courses', [
              'through' =&gt; 'CourseMemberships',
          ]);
      }
  }
  
  class CoursesTable extends Table
  {
      public function initialize(array $config)
      {
          $this-&gt;belongsToMany('Students', [
              'through' =&gt; 'CourseMemberships',
          ]);
      }
  }
  
  class CoursesMembershipsTable extends Table
  {
      public function initialize(array $config)
      {
          $this-&gt;belongsTo('Students');
          $this-&gt;belongsTo('Courses');
      }
  }
  
  Student BelongsToMany Course
  Course BelongsToMany Student
  
  id | student_id | course_id | days_attended | grade
  </code></pre>
  
  <p>How should I construct the query to find Courses for given Student that he has Grade == "A"?</p>
  
  <pre><code>$query = $this-&gt;Courses-&gt;find('all')
      -&gt;contain(['CourseMemberships'])
      -&gt;where(['CourseMemberships.student_id' =&gt; $student['id'], 'CourseMemberships.grade' =&gt; 'A']);
  </code></pre>
  
  <p>This will not work. How should I write it?</p>