在 yii 中使用 CSqlDataProvider 进行排序

2022-01-04 00:00:00 sql php mysql pagination yii

我需要从名为 Hospitals 的表中获取一些具有相同条件的记录:

I need to get some records from a table called Hospitals with the same criteria:

首先获取与 $city 参数匹配的城市.其次获取状态与 $state 参数匹配的那些.第三个按A-Z排序

First get the ones which city match with $city param. Second get the ones which state match with $state param. Third get the rest ordered by A-Z

我需要在一个查询中获取所有这些,因为我要对结果集进行分页,所以我选择 CSqlDataProvider,它接收一个 sql、计数和分页作为初始配置:http://www.yiiframework.com/doc/api/1.1/CSqlDataProvider

I need to get all of them in one query since I'm going to paginate the resulset, so i choose CSqlDataProvider, which receives a sql, the count and the pagination as initial configuration: http://www.yiiframework.com/doc/api/1.1/CSqlDataProvider

所以,我正在考虑将额外的字段作为布尔值并按主题对它们进行排序,例如:same_city、same_state.

so, I was thinking of making extra fields as booleans and sort them by theese, like: same_city, same_state.

所以让我们假设这是进入 CSqlDatProvider 的结果 sql(当我用请求中的参数值替换时):

so let's assume this is the resulted sql (when i replaced with params values from request) that goes to the CSqlDatProvider:

select *, 
IF(city like '%San Antonio%', 1, 0) as same_city, 
IF(state=44, 1, 0) as same_state 
from hospitals 
order by same_city DESC, same_state DESC, hospital_name ASC;

如果我将此字符串分配给 CSqlDataProvider,我将无法分页,因为我正在对订单进行硬编码...但是我需要这个特定的订单,以便记录检索的标准成功.

If i assign this string to the CSqlDataProvider i won't be able to paginate since I'm hardcoding the order... however I need this specific order so the criteria of records retrieving is successful.

$arHospitals = new CSqlDataProvider($sql, array(
    'totalItemCount'=>$count,
    'pagination'=>array(
        'pageSize'=>30,
    ),
));

如果我运行上面的代码,它将返回整个匹配的记录,无论pageSize如何.但是,如果我根据 yii 文档使用分页,我只会得到分页指示的记录,但对我来说是无意义的顺序......我需要字段中的以下顺序:same_city DESC、same_state DESC、hospital_name ASC.

if I run the code above, it will return the whole matching records, no matter of pageSize. However if I use the pagination according to yii documentation, I will get only the records that pagination indicates, but with a non-sense order for me... I need the following order in the fields: same_city DESC, same_state DESC, hospital_name ASC.

$arHospitals = new CSqlDataProvider($sql, array(
    'totalItemCount'=>$count,
    'sort'=>array(
        'attributes'=>array(
            'asc'=>array('hospital_name'),
            'desc'=>array('same_city', 'same_state'),
        ),
    ),
    'pagination'=>array(
        'pageSize'=>30,
    ),
));

关于如何解决这个问题的任何想法?如果我遗漏了有关问题的一些关键信息,请告诉我.

Any ideas of how to solve this? If I'm missing some key information about the problem please let me know.

推荐答案

这样的事情应该可行:

$arHospitals = new CSqlDataProvider($sql, array(
    'totalItemCount'=>$count,
    'sort'=>array(
        'attributes'=>array(
            'virtualFieldName'=>array( //virtual field name
                //give no possibility to sort in any other order than
                'asc'=>'same_city DESC, same_state DESC, hospital_name ASC', 
                'desc'=>'same_city DESC, same_state DESC, hospital_name ASC', 
                'label'=>'Default Sort Order'
            ),
        ),
        'defaultOrder'=>array(
            'virtualFieldName'=>CSort::SORT_ASC, //default sort value
        ),
    ),
    'pagination'=>array(
        'pageSize'=>30,
    ),
));

参见 http://www.yiiframework.com/doc/api/1.1/CSort#attributes-detail 了解更多信息.

See http://www.yiiframework.com/doc/api/1.1/CSort#attributes-detail for more info.

相关文章