title: CakePHP find Queries with aliases SQLSTATE[42S22] Error
body: <p>I'm having a weird problem with my relationships/aliases in CakePHP and now its preventing me from accessing my data correctly.</p>
  
  <p>I have:</p>
  
  <ul>
  <li>User hasMany CreatorModule (alias for Module)</li>
  <li>User HABTM LearnerModule (alias for Module)</li>
  <li>Module belongsTo Creator (alias for User)</li>
  <li>Module HABTM Learner (alias for User)</li>
  </ul>
  
  <p>And I'm trying to call:</p>
  
  <pre><code>$id = $this-&gt;Module-&gt;User-&gt;findByEmail($email);
  $modules = $this-&gt;Module-&gt;findByUserId($id['User']['id']);
  </code></pre>
  
  <p>The queries that get generated aren't correct - the table-alias is wrong. I'm not sure which of the above is responsible but I get:</p>
  
  <pre><code>SELECT
      `Creator`.`id`,
      `Creator`.`email`,
      `Creator`.`organization`,
      `Creator`.`name`,
      `Creator`.`password`,
      `Creator`.`verified`,
      `Creator`.`vcode`
  FROM
      `snurpdco_cake`.`users` AS `Creator` 
  WHERE
      `User`.`email` = 'foo@example.com' # &lt;--
  LIMIT 1
  </code></pre>
  
  <p>I figured out that the error is that CakePHP <i>should</i> change 'User' in the WHERE clause to Creator, but doesn't, even if I use the alias. How do I complete this query correctly.</p>
  
  <p>Further, as a related problem, I find that I can no longer use User in my model calls etc now that I have defined aliases. Is there a way around this?</p>
  
  <p>EDIT: As requested, here is my model code defining the aliases:</p>
  
  <pre><code>class User extends AppModel {
  public $name = 'User';
  public $uses = 'users';
  public $hasMany = array(
      'OwnedModule' =&gt; array(
          'className' =&gt; 'Module',
          'foreignKey' =&gt; 'user_id',
          'dependent' =&gt;  true
          ));
  public $hasAndBelongsToMany = array(
      'LearnerModule' =&gt; array( 
         'className'              =&gt; 'Module',
         'joinTable'              =&gt; 'modules_users',
         'foreignKey'             =&gt; 'user_id',
         'associationForeignKey'  =&gt; 'module_id',
         'unique'                 =&gt; 'keepExisting',
      ));
  //The rest of the Model
  } 
  //Different file, condensed here for spacing 
  class Module extends AppModel {
  public $name = 'Module';
  public $belongsTo = array(
      'Creator' =&gt; array(
          'className' =&gt; 'User'));    
  public $hasAndBelongsToMany = array(
      'Learner' =&gt; array(
         'className'              =&gt; 'User',
         'joinTable'              =&gt; 'modules_users',
         'foreignKey'             =&gt; 'module_id',
         'associationForeignKey'  =&gt; 'user_id',
         'unique'                 =&gt; 'keepExisting',
      ));
  //The rest of the Model
  }
  </code></pre>