Yii2:如何按升序排序时,把空值到对象列表的末尾? [英] Yii2: How to put null values to the end of an object list when sorting is ascending?
本文介绍了Yii2:如何按升序排序时,把空值到对象列表的末尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的DataProvider:
Here is my DataProvider:
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => ['pageSize' => 50],
'sort' => [
'defaultOrder' => [
'priority' => SORT_DESC,
'date_targeted' => SORT_ASC
]
]
]);
我要的是移动(未设置)的结果的末尾。
What I want is to move ("not set") to the end of the results.
我怎样才能做到这一点?
How can I do this?
推荐答案
您可以在添加新字段选择
。而在设置IF
情况下,这样的:
You may add new field in select
. And set in IF
case like that:
$query->select([
'*',
new \yii\db\Expression('IF(date_targeted IS NULL, 1, 0) AS date_targeted_flag')
]);
而在排序
添加 date_targeted_flag
:
'sort' => [
'defaultOrder' => [
'date_targeted_flag' => SORT_ASC,
'priority' => SORT_DESC,
'date_targeted' => SORT_ASC
],
'attributes' => [
'date_targeted_flag',
'priority',
'date_targeted'
]
]
这工作正常 - rel="nofollow">和的这里。
这篇关于Yii2:如何按升序排序时,把空值到对象列表的末尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文