Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top