CSqlDataProvider + CGridView


I need to use CSqlDataProvider to fill the data in a CGridView, because I’m using a complex query

Is it kind of working … I still need to specify columns

Any example of how to do it ?

Also I need to use filter in this grid … is it possible ? if yes how ?

Thanks a lot in advance

Ps: I wont post my code here because it has no relevance, I just need an example

Solved by using CDbCriteria to create the query


Would you mind posting your solution? Many thanks!


its a big piece of code, but I’ll explain it

first transform the code into criteria, use in the select "as" to define the keys names

then define the keys as public variables of the model and use it in the compare method (also add it to the rules as safe on search)

then in the grid view use the defined keys as columns

here is the code of how I did

declare the variables that you will use later

public $e_Tipo,$e_Serial,$e_Marca,$e_Modelo;

public $u_Nombre_Completo,$u_Sede,$u_Cargo;

Search function

public function searchRel()


		$criteria=new CDbCriteria;








            	usuarios.Nombre_Completo as u_Nombre_Completo,

            	usuarios.Cargo as u_Cargo,

            	usuarios.Sede as u_Sede,

            	tipo_equipos.Tipo as e_Tipo,

            	tipo_equipos.Serial as e_Serial,

            	tipo_equipos.Marca as e_Marca,

            	tipo_equipos.Modelo as e_Modelo';


















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




the relations

public function relations()


		return array(


        	'tipo_equipos'=>array(self::BELONGS_TO,'TipoEquipos','','on'=>'t.OC=tipo_equipos.OC'),//the foreign key is not primary



the grid

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




















function actionConsulta1(){

	$model=new ControlReposHabUne('search');









I use the same approach for my grids. But I can’t make these columns sortable. Do you have the same problem? If so, any solutions?

I wasn’t able to make it sortable, but didn’t try much cuz it was not in my todo list

Hi Gustavo,

I have been revisiting my code again and there is a fundamental difference between your example and my data. You are only referring to data that have relations within your model, whilst my original SQL code grabs info from other places as well. Let me show you how it currently is set up:

                        $sql='SELECT `profiles`.phone,


			`profiles`.`name` as user_name,




			course_event.title as event_title,



			FROM users INNER JOIN `profiles` ON users.id = `profiles`.user_id

			 INNER JOIN course_event_registration ON course_event_registration.user_id = users.id

			 INNER JOIN course_event ON course_event_registration.event_id = course_event.id

			 INNER JOIN course_module ON course_event.module_id = course_module.id';

The model on this scenario is the course_event_registration, which only has relations to users and course_event.

Would this work if transformed to criteria? Here’s how I am implementing the widget:

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


	'cssFile' => Yii::app()->baseUrl . '/css/gridViewStyle/gridView.css',

	'pager' => array('cssFile' => Yii::app()->baseUrl . '/css/gridViewStyle/gridView.css'),










                'filter'=>CHtml::listData(CourseEvent::model()->findAll(), 'id', 'title'), // fields from modules table






Any insights on how to take this matter further?

Many thanks,


and what about if your sql statement has a union all from another model? no relationships.

Please check this link http://www.yiiframework.com/forum/index.php/topic/40156-how-to-filter-data-in-cgridview-when-records-get-from-different-models/

and send me the suggestion