title: How do I query data in CakePHP using HABTM relationships?
body: <p>I'm working on a CakePHP 1.2 application. I have a model "User" defined with a few HABTM relationships with other tables through a join table.</p>
  
  <p>I'm now tasked with finding User information based on the data stored in one of these HABTM tables. Unfortunately, when the query executes, my condition is rejected with an error about a missing table. Upon inspection it seems that CakePHP is not including any of the HABTM tables in the select statement.</p>
  
  <p>My User HABTM relationship is as follows:</p>
  
  <pre><code>    var $hasAndBelongsToMany = array(
      'Course' =&gt; array(
          'className'             =&gt; 'Course',
          'joinTable'              =&gt; 'course_members',
          'foreignKey'             =&gt; 'user_id',
          'associationForeignKey'  =&gt; 'course_id',
          'conditions'             =&gt; '',
          'order'                  =&gt; '',
          'limit'                  =&gt; '',
          'uniq'                   =&gt; false,
          'finderQuery'            =&gt; '',
          'deleteQuery'            =&gt; '',
          'insertQuery'            =&gt; ''
      ),
      'School' =&gt; array(
          'className'             =&gt; 'School',
          'joinTable'              =&gt; 'school_members',
          'foreignKey'             =&gt; 'user_id',
          'associationForeignKey'  =&gt; 'school_id',
          'conditions'             =&gt; '',
          'order'                  =&gt; '',
          'limit'                  =&gt; '',
          'uniq'                   =&gt; false,
          'finderQuery'            =&gt; '',
          'deleteQuery'            =&gt; '',
          'insertQuery'            =&gt; ''
      ),
      'Team' =&gt; array(
          'className'             =&gt; 'Team',
          'joinTable'              =&gt; 'team_members',
          'foreignKey'             =&gt; 'user_id',
          'associationForeignKey'  =&gt; 'team_id',
          'conditions'             =&gt; '',
          'order'                  =&gt; '',
          'limit'                  =&gt; '',
          'uniq'                   =&gt; false,
          'finderQuery'            =&gt; '',
          'deleteQuery'            =&gt; '',
          'insertQuery'            =&gt; ''
      )
  );
  </code></pre>
  
  <p>The error is:</p>
  
  <blockquote>
    <p>SQL Error: 1054: Unknown column
    'School.name' in 'where clause'</p>
  </blockquote>
  
  <p>And finally, the query it is trying to execute</p>
  
  <pre><code>     SELECT 
   `User`.`id`, `User`.`username`, `User`.`password`, `User`.`firstName`, `User`.`lastName`, `User`.`email
  
  `, `User`.`phone`, `User`.`streetAddress`, `User`.`city`, `User`.`province`, `User`.`country`, `User
  
  `.`postal`, `User`.`userlevel`, `User`.`modified`, `User`.`created`, `User`.`deleted`, `User`.`deleted_date
  
  ` FROM `users` AS `User`   WHERE `User`.`id` = 6 AND `School`.`name` LIKE '%Test%'    LIMIT 1
  </code></pre>