I have a statistical query in a model’s relations called ‘rating’ that returns a SUM calculation of a field in a related model. Now I want to sort the items in my DataProvider based on this statistical query. I can call $data->rating inside the item view, but using ‘rating’ in ‘defaultOrder’ results in the following error:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'rating' in 'order clause'
Relevant lines in the ‘Idea’ model relations function:
'votes' => array(self::HAS_MANY, 'Vote', 'idea_id'),
'rating' => array(self::STAT, 'Vote', 'idea_id', 'select'=>'SUM(vote)'),
The DataProvider in question:
$DataProvider=new CActiveDataProvider('Idea',array(
'criteria'=>array(
'condition'=>'challenge_id=:challenge',
'params'=>array(':challenge'=>$model->id),
),
'sort'=>array(
'defaultOrder'=>'createTime DESC',
),
'pagination'=>array(
'pageSize'=>30,
),
));
How can I change ‘defaultOrder’ to something like ‘rating ASC, createTime DESC’?
I need to order the items based on the rating field. Tried adding ‘with’=> array(‘rating’), to the criteria but that didn’t help.