I have this search function in my Skill model(used for CGridView in one of my views):
public function searchWithHierarchy()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('value',$this->value);
$criteria->compare('hierarchy',$this->hierarchy);
//original query - select IFNULL(Round(((SUM(ROUND((student_skills.value/skill.value)*100,0)))/(select COUNT(*) from user)),0),0) as successRate from skill left JOIN student_skills on skill.id = student_skills.skill_id group by skill.name
$criteria->select = 'IFNULL(Round(((SUM(ROUND((student_skills.value/skill.value)*100,0)))/(select COUNT(*) from user)),0),0) as successRate';
$criteria->join = 'left JOIN student_skills on skill.id = student_skills.skill_id';
$criteria->group = "skill.name";
$criteria->compare('successRate',$this->successRate);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>25,
),
'sort'=>array(
'defaultOrder'=>array(
'hierarchy'=>CSort::SORT_ASC
)
),
));
}
I wanted to have computed column ‘success rate’ in CGridView (along with other skill columns like name, value…) but I also wanted to sort and filter it like all the other columns.
But $criteria->select is throwing exception:
Active record "Skill" is trying to select an invalid column "IFNULL(Round(((SUM(ROUND((student_skills.value/skill.value)*100". Note, the column must exist in the table or be an expression with alias.
I think it’s because of the comma that’s there but I can’t change the query.
Can anyone suggest how I should change my search function so that I can have in CGridView column ‘success rate’ that is sortable and filterable?
Note: the query is correct