Answer the question
In order to leave comments, you need to log in
GridView and filter via hasOne?
I display the user_id field in the GridView
[
'attribute' => 'user_id',
'options' => array('width'=>'200px'),
'value' => function ($model) { return $model->destinationUser->username; },
]
public function getDestinationUser()
{
return $this->hasOne(\app\modules\user\models\User::className(), ['id' => 'user_id']);
}
Answer the question
In order to leave comments, you need to log in
Let's say you output the model Foo , the crud is generated using gii .
1. Add the $destinationUser property to the FooSearch class :
class FooSearch extends Foo
{
public $destinationUser;
public function rules()
{
return [
...
['destinationUser', 'safe'],
...
];
}
public function search($params)
{
$query = Foo::find();
$query->joinWith(['destinationUser']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
// Для сортировки по User->username
$dataProvider->sort->attributes['destinationUser'] = [
'asc' => ['user.username' => SORT_ASC],
'desc' => ['user.username' => SORT_DESC],
];
...
$query->andFilterWhere([
...
])
// Для поиска по имени
->andFilterWhere(['like', 'user.username', $this->destinationUser]);
return $dataProvider;
}
[
'attribute' => 'destinationUser',
'options' => ['width'=>'200px'],
'value' => 'destinationUser.username',
]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question