$dataProvider = new ActiveDataProvider([
'query' => UserProfile::find()->joinWith(['user'])->where(['<>', 'user.status', 0]),
'sort' => ['attributes' => ['fullname',
'phone',
'user.username' => [
'asc' => ['user.username' => SORT_ASC],
'desc' => ['user.username' => SORT_DESC],
'default' => SORT_DESC
],
'user.email' => [
'asc' => ['user.email' => SORT_ASC],
'desc' => ['user.email' => SORT_DESC],
'default' => SORT_DESC
],
]]
]);
排序fullname
和phone
工作良好。但我想在username
和email
列上添加排序。
请纠正我。
User 和 UserProfile 表有关系。(user_profile.user_id = user.id)
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Username',
'value' => 'user.username',
],
[
'label' => 'Email',
'value' => 'user.email',
],
'fullname',
'address',
'phone',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>