1 Model 2 Cgridviews 2 Orders

Ok , this shouldn’t be complicated , but I haven’t seen anyone providing it:

For one model I’m displaying 2 same CgridViews in different places. In one place I want one column to be sorted by default , and in the other place another column.

Now , I could add sort defaultOrder in the model , but than it’ll sorf both the grids in the same way.

How can I set 2 different sorting orders?

What if for the second place where you want to sort it according to some order, you use a query from the same model, sort according to whatever way you want, then display the returned data in a gridview.

You will have two gridviews, but the first one, will have the model as the data provider, and the second will use a query as a data provider.

I guess you mean SQL query? How can I sort with that?

Sql queries have an Order By command which lets you select a column by which to sort the records with.

Let say i have a table called staff, with a column age, then in Yii, using the query builder, you would sort the staff according to age, with a query like this one.


<?php	

	$command = Yii::app()->db->createCommand();	

	$staff = $command->select('*')

	->from('staff')

	->order('age, desc')	

	->queryAll();

	$provide= new CArrayDataProvider($staff, array(

		

	)); ?>

The above query would then return all the information, in the staff table, ordered by their age.

Notice the


$provide= new CArrayDataProvider($staff, array(

		

	));

is not part of the query. That is how you create a new dataprovider, which you can then use in the CGridView like this,


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

	'dataProvider'=>$provide,	

				 ),	

	

)); ?>

Hope this helps. :rolleyes:

At the end I copied my search , because I passed some params with it,but thats a very interesting solution. Had no idea I can do such.

Thanks,

Mark.