cgridview and group by

Hello All,

Example Database : Table (country,user).

controller:


 $model=new MyModel('search');

        $model->unsetAttributes();  // clear any default values

        $criteria = new CDbCriteria;

        

     if (isset($_GET['MyModel']))

            $model->attributes = $_GET['MyModel'];

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

            'criteria' => $criteria,

        ));  

View :




$this->widget('bootstrap.widgets.TbGridView', array(

    'id' => 'grid-unassigned',

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

    'filter'=>$model,

    

    'columns' => array(




'country',

'user,

                        

    ),

));

?>



My question :

How can I display the view grouped by country : something like :

country 1 user 1

           user2


           user3

country 2 user4

           user5

Please Help.

Regards.

Put in your controller




        $criteria->group = 'country';



after defining $criteria, so:




 $model=new MyModel('search');

        $model->unsetAttributes();  // clear any default values

        $criteria = new CDbCriteria;


        //

        // Order By Country

        //

        $criteria->group = 'country';

        

     if (isset($_GET['MyModel']))

            $model->attributes = $_GET['MyModel'];

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

            'criteria' => $criteria,

        ));  



Are you using MySQL ?

Hello Fabrizio Caldarelli and thank you for your answer.,

Yes I am using Mysql… But, $criteria->group = ‘country’ didn’t work!

Thanks

You are right!

I haven’t seen that in your view you don’t use $dataProvider from your controller, but use $model->search()!

So you should put in your view:




$this->widget('bootstrap.widgets.TbGridView', array(

    'id' => 'grid-unassigned',

   'dataProvider' => $dataProvider,

    'filter'=>$model,

    

    'columns' => array(




'country',

'user,

                        

    ),

));

?>



But in this way you lost all compare() that are in search() of model.

I usually split search() in two methods: searchCriteria() and search(). With searchCriteria() I have $criteria object to be used also in grid.

For example, in my custom model:




	public function searchCriteria()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new \CDbCriteria;


$criteria->compare('country',$this->country);

$criteria->compare('user',$this->user,true);


		return $criteria;

	}


	public function search()

	{

		$criteria=$this->searchCriteria();


		return new \CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



So in actionGrid I can use:




 $model=new MyModel('search');

        $model->unsetAttributes();  // clear any default values




        

     if (isset($_GET['MyModel']))

            $model->attributes = $_GET['MyModel'];


        $criteria = $model->searchCriteria();

        $criteria->group = 'country';


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

            'criteria' => $criteria,

        )); 



Thanks again, It is working now but not the way I want it, I am looking to display it this way but without using extension :

My link

What extennsion? I haven’t used extensions.

Check My link Please