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.

我对此进行了大量搜索,到目前为止还没有得到答案。 这是问题:
我正在创建CDbCriteria对象以获取正确的数据: p>

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

其中 $ arrayContents code>是一个由以下元素组成的数组: p>

  Array([0] => 10 [1] => 16 [2] => 14 [3] => 15)
  code>  pre> 
 \  n 

这些是我想要使用dataProvider呈现的 id_posts code>,但仅限于特定的顺序。 此外,我以这种方式创建dataProvider: p>

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

显示 id_posts的顺序 代码>是:10,14,15,16。不是我想要的那个。
我也尝试过这样创建dataProvider: p>

  $ dataProviderPosts = 新的CActiveDataProvider('帖子',数组(
'标准'=> $ criteria,
'排序'=>数组(
'defaultOrder'=>数组('id_post'=> $ arrayContents),  
),
'pagination'=> array(
'pageSize'=> 20,
),
)); 
  code>  pre> 
 
 

但它只按顺序显示它:16,15,14,10,所以只是向后,所以不是我想要的,因为我想按顺序显示它:10,16,14,15就像 $ arrayContents 代码> p> div>

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.