Newbie Conceptual Issue - Cactivedataprovider Many To Many

Hi Everyone!

I’m having a bit of an issue with conceptually understanding the way the data grid works and it would be great to get some help please :slight_smile:

I have tables as follows:




CREATE TABLE `report` (

  `REPORT_ID` double(8,0) NOT NULL AUTO_INCREMENT,

  `REPORT_TITLE` varchar(250) NOT NULL,

  `REPORT_ITEM_ID` double(8,0) NOT NULL

  PRIMARY KEY (`REPORT_ID`)

);


CREATE TABLE `report_item` (

  `REPORT_ITEM_ID` double(8,0) NOT NULL AUTO_INCREMENT,

  `REPORT_ID` double(8,0) NOT NULL,

  `ITEM_ID` double(8,0) NOT NULL

  PRIMARY KEY (`REPORT_ITEM_ID`)

);


CREATE TABLE `item` (

  `ITEM_ID` double(8,0) NOT NULL

  `ITEM_NAME`varchar(250)

  PRIMARY KEY (`ITEM_ID`)

);



A report can have many report items (though in practice in almost all cases exactly 1).

I’d like to create a zii.widgets.grid.CGridView form that displays the report id, the report title and the item name and then 2 extra selectable form columns for users to interact with.

I’ve read through these:

http://www.yiiframework.com/doc/api/1.1/CGridView

but I don’t think I’m really understanding the concept properly.

If I do the following:




$model=Report::model();


$criteria=new CDbCriteria();

$criteria->select="`t`.REPORT_ID, REPORT_TITLE, ITEM_NAME";

$criteria->order="REPORT_ID ASC";

$criteria->alias="t";

$criteria->join="LEFT JOIN `report_item` ON `t`.`REPORT_ID` = `report_item`.`REPORT_ID` LEFT JOIN `item` ON `item`.`ITEM_ID` = `report_item`.`ITEM_ID`";


$dataProvider = new CActiveDataProvider($this, array(

        'criteria' => $criteria

    ));


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

    'id' => 'report-grid',

    'dataProvider' => $dataProvider ,

    'enablePagination' => true,

    'columns' => array(

		array('name'=>'REPORT_ID','htmlOptions'=>array('width'=>'5%')),

		'REPORT_TITLE',

     	        'ITEM_NAME'

	)

));



I get no results, but also no errors. Variations of the same don’t appear to be working.

Does anyone have any good references with lots of examples?

Thanks!!

Ok, I’ve made a little bit of progress…

In the controller I have:




public function actionReportPage()

	{

		$model = new Report('search');

		$model->unsetAttributes();

		

		$dataProvider=new CActiveDataProvider($model,

                    array(

                      'pagination'=>array('pageSize'=>15),

                      'criteria'=>array('with'=>array('reportItemss'),'together'=>true),

                    )

                 );

		

		$this->render('myReportPage', array(

			'dataProvider'=>$dataProvider

		));

	}



Then in the view I have:




$form=$this->beginWidget('CActiveForm', array(

	'id'=>'report-form',

	'enableAjaxValidation'=>false,

)); 


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

    'id' => 'report-grid',

    'dataProvider' => $dataProvider ,

    'enablePagination' => true,

    'columns' => array(

		array('name'=>'REPORT_ID','htmlOptions'=>array('width'=>'5%')),

		'REPORT_TITLE',

		'ITEM_NAME' => array('name'=>'reportItemss.iTEM.ITEM_NAME')

	)

));


$this->endWidget();



It’s a bit closer to what I’m after, but I’m not getting any item names out…

Progress! It’s basically displaying how I’d like it now! (almost)

Controller:




public function actionReportPage()

	{

		$model = new Report('search');

		$model->unsetAttributes();

		

		$dataProvider=new CActiveDataProvider($model,

                    array(

                      'pagination'=>array('pageSize'=>15),

                      'criteria'=>array('with'=>array('reportItemss'),'together'=>true),

                    )

                 );

		

		$this->render('myReportPage', array(

                        'model' => $model,        // added in this line

			'dataProvider'=>$dataProvider

		));

	}



view:




$form=$this->beginWidget('CActiveForm', array(

	'id'=>'report-form',

	'enableAjaxValidation'=>false,

)); 


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

    'id' => 'report-grid',

    'dataProvider' => $dataProvider ,

    'filter' => $model,

    'enablePagination' => true,

    'columns' => array(

		array('name'=>'REPORT_ID','htmlOptions'=>array('width'=>'5%')),

		'REPORT_TITLE',

		array('header'=>'Item Name','value'=>'$data->reportItemss ? $data->reportItemss[0]->iTEM->ITEM_NAME : ""'),

                array('header'=>'True/False',

		     'value'=>'CHtml::radioButtonList("status",\'$data->STATE\',array("0"=>"True","1"=>"False","2"=>"Undecided"))',

			'type'=>'raw',

		),


	)

));


$this->endWidget();