I have a CGridView
which calls search
(AFAIK) on the given model to fetch the list of models to display. Is it possible to order the result of said view by the result of calling $model->some_method()
?
I've read a bunch of similar questions and they all pretty much recommend to duplicate what the code does in SQL, for example here. The problem is that the some_method
is much more complicated and cannot be written in SQL.
I've also read about virtual attributes, which would fit nicely, but if I write:
public function getSomeMethod() { ... }
and then use:
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>false,
'sort' => array(
'attributes' => array('somemethod'),
'defaultOrder' => array(
'somemethod' => CSort::SORT_ASC,
)
)
));
it gives me:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't.somemethod' in 'order clause'. The SQL statement executed was: SELECT * FROM
use_case
t
ORDER BYt
.somemethod
What should I do?
The defaultOrder array content must be an array of columns related to the CSort::ConstantValue ..
see yii1 doc http://www.yiiframework.com/doc/api/1.1/CSort#defaultOrder-detail
'attributes'=>array(
'price'=>array(
'asc'=>'item.price',
'desc'=>'item.price DESC',
'label'=>'Item Price',
'default'=>'desc',
),
'*',
)
Then if you need sort for a method result you simply set this result in proper column and the set this column name in array ..
If you don't can define the method like a sql statement you can't use the order of the dataprovider
the problem is that the default order and in general the order are clause managed by sql and not by yii or php then if you can't define the method like sql element you can't use this for ordering in dataProvider .. essentially dataProvider in a sql manager .