title: Yii add filter to a virtual attribute in CGridView and make it sortable
body: <p>I have the following Models :</p>
  
  <p><code>User</code> with columns {id,user_name,password,user_type}</p>
  
  <p><code>Admin</code> with columns {id,user_id,full_name,.....etc}</p>
  
  <p><code>Editor</code> with columns {id, user_id,full_name,...etc}</p>
  
  <p>and the relations are
  <code>User</code> : <code>'admin' =&gt; array(self::HAS_ONE, 'Admin', 'user_id'),'editor' =&gt; array(self::HAS_ONE, 'Editor', 'user_id'),</code></p>
  
  <p><code>Admin</code> : <code>'user' =&gt; array(self::BELONGS_TO, 'User', 'user_id'),</code></p>
  
  <p><code>Editor</code> : <code>'user' =&gt; array(self::BELONGS_TO, 'User', 'user_id'),</code></p>
  
  <p>Now i had setup a virtual attribute <code>fullName</code> in <code>User</code> Model as below</p>
  
  <pre><code>public function getFullName()
  
  {
  
      if($this-&gt;user_type=='admin')
  
          return $this-&gt;admin-&gt;full_name;
  
      else if($this-&gt;user_type=='editor')
  
          return $this-&gt;editor-&gt;full_name;
  
  
  }
  </code></pre>
  
  <p>I can show the virtual attribute , <code>fullName</code> , in a gridview , But how do i add a filter to the attribute and make it sortable in the gridview?</p>
  
  <p><strong>UPADTE 1:</strong> </p>
  
  <p>I updated the models search() function as per the answer by @Jon as shown below </p>
  
  <pre><code>    public function search()
          {
  
  
              $criteria=new CDbCriteria;
              $criteria-&gt;select=array('*','COALESCE( editor.full_name,admin.first_name, \'\') AS calculatedName');
              $criteria-&gt;with=array('editor','admin');
              $criteria-&gt;compare('calculatedName',$this-&gt;calculatedName,true);
              $criteria-&gt;compare('email',$this-&gt;email,true);
              $criteria-&gt;compare('user_type',$this-&gt;user_type);
  
              return new CActiveDataProvider($this, array(
                  'criteria'=&gt;$criteria,
  
  
  ));
      }
  </code></pre>
  
  <p>The names of both admins and editors are shown correctly in the gridview. But when i do a search through the filter the following exception occurs,</p>
  
  <pre><code>CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'calculatedName' in 'where clause'. The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`id`) FROM `user` `t`  LEFT OUTER JOIN `editor` `editor` ON (`editor`.`user_id`=`t`.`id`)  LEFT OUTER JOIN `admin` `admin` ON (`admin`.`user_id`=`t`.`id`)  WHERE (calculatedName LIKE :ycp0) (C:\xampplite\htdocs\yii\framework\db\CDbCommand.php:528)&lt;/p&gt;&lt;pre&gt;#0 C:\xampplite\htdocs\yii\framework\db\CDbCommand.php(425):
  </code></pre>
  
  <p>How can I get rid of this?</p>
  
  <p>UPDATE 2:
  My mistake. It works fine when i changed the line</p>
  
  <pre><code>$criteria-&gt;compare('calculatedName',$this-&gt;calculatedName,true);
  </code></pre>
  
  <p>to</p>
  
  <pre><code>$criteria-&gt;compare('COALESCE( editor.full_name,admin.first_name, \'\')',$this-&gt;calculatedName,true);
  </code></pre>
  
  <p>and btw thanx @Jon.</p>