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 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同