Dataprovider and GridView

Hi there,

Can anyone guide me please how to create a custom sort filter(say):‘rating’ which is not stored in database but only calculated in my model.Rest all the columns are working fine with GridView widget and ActiveDataProvider but I’m confused to enable sorting for this particular field:

In Grid view I’m able to customize the column i.e


‘header’ => ‘<a class="" data-sort=“cat_name” href="">Rating</a>’,

‘attribute’ => ‘rating’,

‘value’ => function($model) {

return &#036;model-&gt;averagerating(&#036;model-&gt;id);


‘filter’ => Html::textInput(‘UserSearch[rating]’, $name, [‘class’=>‘form-control’]) ,


But I’m not able to manipulate the db records.

Try use yii\data\Sort, check here:

With this it is possible to sort attributes that don’t come from the database, you can even use this directly with your data provider through the method setSort() (check here)