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.

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

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?