doutangshuan6473 2013-05-20 14:34
浏览 220
已采纳

Yii dataProvider由数组给出的数据顺序

I've searched a lot about that and didn't got the answer so far. Here's the problem:
I'm creating CDbCriteria object to get proper data:

$criteria = new CDbCriteria;
$criteria->compare('id_post', $arrayContents);

where $arrayContents is an array consisting of the following elements:

Array ( [0] => 10 [1] => 16 [2] => 14 [3] => 15 )

Those are the id_posts I want to present using dataProvider, but only in that specific order. Further i create dataProvider this way:

$dataProviderPosts = new CActiveDataProvider('Posts',array(
            'criteria'   => $criteria,
            'pagination'=>array(
                    'pageSize'=>20,
            ),
    ));

The order of displayed id_posts is: 10, 14, 15, 16. Not the one that i wanted.
I've also tried creating dataProvider like that:

$dataProviderPosts = new CActiveDataProvider('Posts',array(
            'criteria'=>$criteria,
            'sort'=>array(
                    'defaultOrder'=>array('id_post'=>$arrayContents),
            ),
            'pagination'=>array(
                    'pageSize'=>20,
            ),
    ));

But it only did displaying it in that order: 16, 15, 14, 10, so just backward, so not what I wanted, because I want to display it in order: 10, 16, 14, 15 just like numbers in $arrayContents.

  • 写回答

1条回答 默认 最新

  • dongshu7162 2013-05-20 16:23
    关注

    The CActiveDataProvider wont do what you need. The order it provides is just the plain SQL order, it has to be computed as a combination of columns ASC or DESC.

    The order you want looks like an arbitrary order of ids. I guess you don't have other columns like 'position' or so in your model, so you'll have to sort your data yourself.

    I didn't try but after the API, you may use the getData() / setData() methods in order to extract data, sort it and set it back in your provider.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)