Dear Friend
I just simulated your scenario.
I have a model Identity(id,age,first_name,last_name,family_name) .just made for an example.
MODEL
public $full_name;
public function rules()
{
return array(
...................................................................................
array('id, age, first_name, last_name, family_name,full_name', 'safe', 'on'=>'search'),
);
}
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->select="*,IF(age>40,CONCAT(first_name,' ',family_name),CONCAT(first_name,' ',last_name)) AS full_name";
$criteria->compare('id',$this->id);
$criteria->compare('age',$this->age);
$criteria->compare('first_name',$this->first_name,true);
$criteria->compare('last_name',$this->last_name,true);
$criteria->compare('family_name',$this->family_name,true);
$criteria->compare("IF(age>40,CONCAT(first_name,' ',family_name),CONCAT(first_name,' ',last_name))",$this->full_name,true); //YOU CAN USE FILTERS ON THE NEW COLUMN.
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array('attributes'=>array('full_name'=>array(),"*")),//YOU CAN SORT ON THE DERIVED COLUMN.
));
}
VIEW
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'identity-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'age',
'first_name',
'last_name',
'family_name',
'full_name',//JUST ADD THE VIRTUAL PROPERTY.
array(
'class'=>'CButtonColumn',
),
),
)); ?>
We have not only mapped a virtual property to a calculated field in the table.
We also enabled sorting and filtering on the new field as well.
Regards.