This is how you setup search model (I did not include your other columns, so dont forget on those)
You can find more here: http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/
class UserSearch extends User
{
public $name;
public function rules()
{
return [
[['name'], 'safe'],
// some other rules ...
];
}
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
public function search($params)
{
$query = User::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['name'] = [
'asc' => ['lastname' => SORT_ASC, 'firstname' => SORT_ASC],
'desc' => ['lastname' => SORT_DESC, 'firstname' => SORT_DESC],
'label' => 'Name',
'default' => SORT_ASC
];
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
// some other filters ...
$query->andFilterWhere([
'or',
['like', 'lastname', $this->name],
['like', 'firstname', $this->name],
]);
return $dataProvider;
}
}
And in your gridview instead of
[
'attribute'=>'firstname',
// ...
],
just use
[
'attribute'=>'name',
],
searchModel.