title: Working with Virtual Fields when using model alias
body: <p>I'm a CakePHP newbie. I have been looking for an answer to this for a while.</p>
  
  <p>I followed directions from RichardAtHome in answer regarding autocomplete in CakePHP (<a href="https://stackoverflow.com/questions/8206822/autocomplete-cakephp-2-0">autoComplete CakePHP 2.0</a>).</p>
  
  <p>I set up the function in my AppController.</p>
  
  <p>This works very well with real fields but bugged when using Virtual Fields:</p>
  
  <pre><code>class Person extends AppModel {
      public $virtualFields = array(
          'name' =&gt; "CONCAT(Person.firstname, ' ', Person.lastname)"
      );
  }
  </code></pre>
  
  <p>I get this error: <code>Column not found: 1054 Unknown column 'Person.name' in 'where clause'</code></p>
  
  <p>When checking the SQL query I see this: </p>
  
  <pre><code>(CONCAT(`Person`.`firstname`, ' ', `Person`.`lastname`)) AS `Person__name`
  </code></pre>
  
  <p>This problem only occurs when I use <code>$model = $this-&gt;{$this-&gt;modelClass}-&gt;alias;</code>. Hardcording the model class in a specific controller (not AppController) works fine.</p>
  
  <p>What do I need to do to make it work?</p>
  
  <h1>UPDATE:</h1>
  
  <p>After fiddling with this I discovered that it doesn't relate to <code>$model = $this-&gt;{$this-&gt;modelClass}-&gt;alias;</code> at all.</p>
  
  <p>Instead I changed the <code>'conditions'</code> value in the <code>find()</code> method and it all worked out fine. I am still puzzled as to why, but now it works just fine.</p>
  
  <p>Incorrect code:</p>
  
  <pre><code>$result = $this-&gt;$model-&gt;find('all', array(
      'conditions' =&gt; array(
          $model . '.' . $field . " LIKE '%" . $term . "%'"
      )
  ));
  </code></pre>
  
  <p>Correct code:</p>
  
  <pre><code>$result = $this-&gt;$model-&gt;find('all', array(
      'conditions' =&gt; array(
          $model . '.' . $field . " LIKE " =&gt; "%" . $term . "%"
      )
  ));
  </code></pre>