<legend id='fQ74n'><style id='fQ74n'><dir id='fQ74n'><q id='fQ74n'></q></dir></style></legend>
<i id='fQ74n'><tr id='fQ74n'><dt id='fQ74n'><q id='fQ74n'><span id='fQ74n'><b id='fQ74n'><form id='fQ74n'><ins id='fQ74n'></ins><ul id='fQ74n'></ul><sub id='fQ74n'></sub></form><legend id='fQ74n'></legend><bdo id='fQ74n'><pre id='fQ74n'><center id='fQ74n'></center></pre></bdo></b><th id='fQ74n'></th></span></q></dt></tr></i><div id='fQ74n'><tfoot id='fQ74n'></tfoot><dl id='fQ74n'><fieldset id='fQ74n'></fieldset></dl></div>
    <tfoot id='fQ74n'></tfoot>
    1. <small id='fQ74n'></small><noframes id='fQ74n'>

        <bdo id='fQ74n'></bdo><ul id='fQ74n'></ul>
    2. 在 yii 中使用 CSqlDataProvider 进行排序

      时间:2023-09-24

        <tbody id='KPyRg'></tbody>
      1. <i id='KPyRg'><tr id='KPyRg'><dt id='KPyRg'><q id='KPyRg'><span id='KPyRg'><b id='KPyRg'><form id='KPyRg'><ins id='KPyRg'></ins><ul id='KPyRg'></ul><sub id='KPyRg'></sub></form><legend id='KPyRg'></legend><bdo id='KPyRg'><pre id='KPyRg'><center id='KPyRg'></center></pre></bdo></b><th id='KPyRg'></th></span></q></dt></tr></i><div id='KPyRg'><tfoot id='KPyRg'></tfoot><dl id='KPyRg'><fieldset id='KPyRg'></fieldset></dl></div>
      2. <small id='KPyRg'></small><noframes id='KPyRg'>

        1. <legend id='KPyRg'><style id='KPyRg'><dir id='KPyRg'><q id='KPyRg'></q></dir></style></legend>

              <bdo id='KPyRg'></bdo><ul id='KPyRg'></ul>
            • <tfoot id='KPyRg'></tfoot>
                本文介绍了在 yii 中使用 CSqlDataProvider 进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我需要从名为 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.

                这篇关于在 yii 中使用 CSqlDataProvider 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用 Yii 创建 PDF 或 Word 文档? 下一篇:用 AJAX 加载的 CKEditor + Yii:$_POST 不包含更新的值

                相关文章

                <tfoot id='o3FeJ'></tfoot>
                <legend id='o3FeJ'><style id='o3FeJ'><dir id='o3FeJ'><q id='o3FeJ'></q></dir></style></legend>
                1. <small id='o3FeJ'></small><noframes id='o3FeJ'>

                      <bdo id='o3FeJ'></bdo><ul id='o3FeJ'></ul>
                    <i id='o3FeJ'><tr id='o3FeJ'><dt id='o3FeJ'><q id='o3FeJ'><span id='o3FeJ'><b id='o3FeJ'><form id='o3FeJ'><ins id='o3FeJ'></ins><ul id='o3FeJ'></ul><sub id='o3FeJ'></sub></form><legend id='o3FeJ'></legend><bdo id='o3FeJ'><pre id='o3FeJ'><center id='o3FeJ'></center></pre></bdo></b><th id='o3FeJ'></th></span></q></dt></tr></i><div id='o3FeJ'><tfoot id='o3FeJ'></tfoot><dl id='o3FeJ'><fieldset id='o3FeJ'></fieldset></dl></div>