Help with sorting in Yii2 gridview. There is a User model, a UserSearch model.
class User extends \yii\db\ActiveRecord { public function rules() { return [ [['email'], 'string', 'max' => 50], [['phone'], 'string', 'max' => 25], [['username', 'password'], 'string', 'max' => 100], ]; } /* считает рейтинг по заполненным полям в таблице и возвращает результат */ public function getRating(){ ... } } class UserSearch extends User { public $rating; public function rules() { return [ ... [['rating'], 'safe'], ]; } public function search($params){...} } In gridview, I added the attribute 'rating'.
GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ 'rating', ], ]); It displays the rating, which is considered to be in the getRating () method of the User model. But when I click on 'Rating' in the header of the gridview table, I get the error 'Unknown column' rating '.
Most of the gridview sorting articles describe how to do this sorting by related fields. In my case there is no connection with other tables, there is no rating field in the Users table. Here it just takes place the calculation of the rating of the fields in the User table and the output of the result. How to filter and sort by this column in the gridview table?