CArrayDataProvider sorting doesn't work

i have a custom model ApplicationVersion


<?php


class ApplicationVersion extends CModel {


    public $id;

    public $version;

    public $description;


    public function __construct($id = null, $version = null, $desc = null) {

        $this->id = $id;

        $this->version = $version;

        $this->description = $desc;

    }


    public function attributeNames() {

        return array('id', 'version', 'description');

    }


    public function rules() {

        return array(

            array('id, version, description', 'safe'),

        );

    }


    public function search() {

        $rawData = array(

            new ApplicationVersion(1, '1.1', 'verzija 1.1'),

            new ApplicationVersion(2, '1.2', 'verzija 1.2'),

            new ApplicationVersion(3, '2.1', 'verzija 2.1'),

            new ApplicationVersion(4, '3.1', 'verzija 3.1'),

            new ApplicationVersion(5, '4.1', 'verzija 4.1'),

            new ApplicationVersion(6, '5.1', 'verzija 5.1'),

            new ApplicationVersion(7, '6.1', 'verzija 6.1'),

            new ApplicationVersion(13, '1.13', 'verzija 1.13'),

            new ApplicationVersion(8, '7.1', 'verzija 7.1'),

            new ApplicationVersion(9, '8.1', 'verzija 8.1'),

            new ApplicationVersion(10, '1.19', 'verzija 1.19'),

            new ApplicationVersion(11, '1.11', 'verzija 1.11'),

            new ApplicationVersion(12, '1.12', 'verzija 1.12'),

            new ApplicationVersion(14, '1.14', 'verzija 1.14'),

        );


        $config = array(

            'pagination' => array(

                'pageSize' => 10,

            ),

            'sort' => array(

                'attributes' => array(

                    'version', 'description',

                ),

            ),

        );


        return new CArrayDataProvider($rawData, $config);

    }


}


?>



function search returns CArrayDataProvider, where i try to sort my data.

i pass provider to the CGridView with


$this->widget('zii.widgets.grid.CGridView', array(    

    'dataProvider' => $model->search(),

    'filter' => $model,

    'columns' => array(

        'version',

        'description',

    ),

    'htmlOptions' => array('style' => 'width:700px'),

));

when the grid shows, data isn’t sorted

and when i press the column header a messagebox pops up and i get a warning


Parameter 1 to array_multisort() expected to be a reference, value given</p>


<h3>Source File</h3>

<p>

...yii\framework\web\CArrayDataProvider.php(122)</p>


<div class="source">

<pre>

00110:         if(empty($directions))

00111:             return;

00112:         $args=array();

00113:         foreach($directions as $name=&gt;$descending)

00114:         {

00115:             $column=array();

00116:             foreach($this-&gt;rawData as $index=&gt;$data)

00117:                 $column[$index]=is_object($data) ? $data-&gt;$name : $data[$name];

00118:             $args[]=$column;

00119:             $args[]=$descending ? SORT_DESC : SORT_ASC;

00120:         }

00121:         $args[]=&amp;$this-&gt;rawData;

<div class="error">00122:         call_user_func_array('array_multisort', $args);

</div>00123:     }

00124: 

00125:     /**

00126:      * Converts the &quot;ORDER BY&quot; clause into an array representing the sorting directions.

00127:      * @param string the &quot;ORDER BY&quot; clause.

00128:      * @return array the sorting directions (field name =&gt; whether it is descending sort)

00129:      */

00130:     protected function getSortDirections($order)

00131:     {

00132:         $segs=explode(',',$order);

00133:         $directions=array();

00134:         foreach($segs as $seg)

is this a bug or am i doing something wrong?

I copy & paste your code and it works perfect for me.

I have tha last version (1.1.4) of Yii.

Which version do you have?

yii 1.1.4, php 5.3.1

i’m guessing it’s php

ok, i’ve installed php 5.2.14 and sorting works.

but when the grid loads data is unsorted, can i change that so that the grid loads with sorted data?

I just had the same problem with sorting a CArrayDataProvider together with PHP 5.3. So I looked around and found a solution at http://php.net/manual/de/function.array-multisort.php within comment from 20-Jan-2010 11:42.

Following these instructions I modified my CArrayDataProvider.php with:


    

protected function sortData($directions)

    {

        if(empty($directions))

            return;

        $args=array();

        $argsSave=array();

        foreach($directions as $name=>$descending)

        {

            $column=array();

            foreach($this->rawData as $index=>$data)

                $column[$index]=is_object($data) ? $data->$name : $data[$name];

            $argsSave[]=&$column;

            $args[]=&$column;

            unset($column);

            $direction=$descending ? SORT_DESC : SORT_ASC;

            $argsSave[]=&$direction;

            $args[]=&$direction;

            unset($direction);

        }

        $args[]=&$this->rawData;

        call_user_func_array('array_multisort', $args);

    }



It would be fine to include this fix (or a more optimized one) in next version of yii.

What steps need to be done for this???