How to implement a "special" sorting algorithm for CActiveDataProvider?

Hey guys,

at the moment I’m working on some kind of a “news page”. Has lots of posts, which should be sorted in a special order.

There are two columns in the base table after which the entrys should be sorted.

  1. Column "fixedUntil" contains a datetime value (nullable).

  2. Column "timestamp" contains a timestamp value - the creationdate.

Now I want to sort the datasets as follows:

  1. All "fixedUntil" columns != null and > actual time should be on top.

  2. Of these fixed entrys should be the ones on top, which end to be fixed at first.

  3. The rest of the datasets should be ordered by the timestamp (after all the fixedones), newest entries first.

Well… with the condition attribute its probably not possible. In general I would write my own function for this, but I’m wondering if there is a better solution, or if there is some kind of a “Yii-way” for this problem?

Thanks a lot,

Majestic

I finally solved it this way:




public function actionIndex() {

        // Sorting News by timestamp via criteria

        $criteria = new CDbCriteria(array(

                    'order' => 'timestamp DESC',

                ));


        $dataProvider = new CActiveDataProvider('News', array(

                    'pagination' => array(

                        'pageSize' => Yii::app()->params['postsPerPage'],

                    ),

                    'criteria' => $criteria,

                    'sort' => array(),

                ));


        // Initializing the variables to temporary save the fixed and not fixed entrys

        $fixedEntrys;

        $notFixedEntrys;


        foreach ($dataProvider->data as $news) {

            // sort the datasets

            if (strtotime($news->fixedBis) > time()){

                $fixedEntrys[] = $news;

            } else {

                $notFixedEntrys[] = $news;

            }                

        }

        

        // save all datasets in the correct order in $allEntrys variable

        $allEntrys = $fixedEntrys;        

        foreach ($notFixedEntrys as $entry){

            $allEntrys[] = $entry;

        }

        

        // fill the $dataProvider->data Array

        $dataProvider->data = $allEntrys;

              

        $this->render('index', array(

            'dataProvider' => $dataProvider,

        ));

    }



Or is there any better / cleaner solution?

Tanks,

Majestic