Ajax sorting on relational data in CArrayDataProvider

Hi all,

It’s been a couple days since I’m trying to make all my CGridView tables sortable but couldn’t get this one working.

So I have two models: Notes and Users. The relation is the following: Notes.UserId => User.Id

I’m using a CGridView to list all the Notes as follows:




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

      'dataProvider' => new CArrayDataProvider($notes, array(

        'pagination' => array(

          'pagesize' => 10,

          'route' => 'notes/AjaxNotesList',

          'params' => $params,

        ),

        'sort' => array(

          'attributes' => array('user' => array(

                        'asc' => 'user.name',

                        'desc' => 'user.name DESC'

                  )

          ),

          'defaultOrder' => 'id DESC',

          'route' => 'notes/AjaxNotesList',

        )

    )),

    'pager' => array(

      'class'=>'CAjaxLinkPager',

      'updateId' => 'notesListDiv',

      'loadingCssClass' => 'grid-view-loading',

    ),

    'id' => 'notesList',    

    'columns'=>$columns,

    'summaryText'=> 'All review findings which are still open',

  ));



When I click on the user’s name column I get the following exception: ‘Property “Note.user.name” is not defined.’

Here’s my Note model class:




...

 * The followings are the available model relations:

 * @property User $user

 */

...

public function relations()

{

// NOTE: you may need to adjust the relation name and the related

// class name for the relations automatically generated below.

return array(

  'user' => array(self::BELONGS_TO, 'User', 'userId'),

);

}



So as you can see the User property should exist because it is defined in the model.

Do you guys have any suggestions? I went through the forum completely but couldn’t find any solution.

Hi,

you must specify the search function too in your model.

For instance:




...

$criteria->with = array( 'civility', 'sex', 'nationality', 'situation', 'stage', 'language', 'department', 'relStudentClasses', 'relStudentSemesters',);

$criteria->compare( 'language.language_name', $this->language_search, true );

...


		$sort = new CSort( '' );

		$sort->separators = array(',',':');

		$sort->attributes = array(

    			'stage_search'=>array(

           			'asc'=>'stage.stage_name',

           			'desc'=>'stage.stage_name DESC',

   				),

		);

		$sort->multiSort = true;

    	$sort->defaultOrder = 'stage.stage_name, language.language_name, "relStudentSemesters".level, student_lastname, student_firstname';

...

return new CActiveDataProvider(get_class($this), array(

	'criteria'=>$criteria,

	'sort' => $sort, // tri multicriteres

)

);




And i think that you should not specify sort in your grid.

Hi ragua!

Thanks for your quick reply!

I do have a search function in my model but I believe that has nothing to do with listing and sorting some data in a GridView. Anyways I played with the search function, extended it a little bit but had no effect.

I can’t use CActiveDataProvider because I’m collecting the data from several other sources as well with recursion and that requires custom sql so I need to stick with CArrayDataProvider.

Any other idea?

You are right, your dataprovider contains almost all the elements of the model description. Only the ‘together’ property, used in model to force the reading of related model with the main didn’t appears,but i don’t see where you can specify this.