HOW to get Detail Data from Relations

Hi, I have this models

This is the header:


/**

 * This is the model base class for the table "t_cuota_def".

 * DO NOT MODIFY THIS FILE! It is automatically generated by giix.

 * If any changes are necessary, you must set or override the required

 * property or method in class "CuotaDef".

 *

 * Columns in table "t_cuota_def" available as properties of the model,

 * followed by relations of table "t_cuota_def" available as properties of the model.

 *

 * @property string $id

 * @property integer $fk_grado_id

 * @property string $vigencia_desde

 * @property integer $fk_cuenta_contable_id

 *

 * @property TGrado $fkGrado

 * @property TCuotaDefDetalle[] $tCuotaDefDetalles

 */

abstract class BaseCuotaDef extends GxActiveRecord {


        ...

        ...


	public function relations() {

		return array(

			'fkGrado' => array(self::BELONGS_TO, 'Grado', 'fk_grado_id'),

			'tCuotaDefDetalles' => array(self::HAS_MANY, 'CuotaDefDetalle', 'fk_cuota_def_id'),

		);

	}


}

and This is the detail:




/**

 * This is the model base class for the table "t_cuota_def_detalle".

 * DO NOT MODIFY THIS FILE! It is automatically generated by giix.

 * If any changes are necessary, you must set or override the required

 * property or method in class "CuotaDefDetalle".

 *

 * Columns in table "t_cuota_def_detalle" available as properties of the model,

 * followed by relations of table "t_cuota_def_detalle" available as properties of the model.

 *

 * @property string $id

 * @property string $fk_cuota_def_id

 * @property string $fk_concepto_cuota_id

 * @property string $importe

 *

 * @property TCuotaDef $fkCuotaDef

 * @property TCuotaConceptos $fkConceptoCuota

 */

abstract class BaseCuotaDefDetalle extends GxActiveRecord {


        ...

	public function relations() {

		return array(

			'fkCuotaDef' => array(self::BELONGS_TO, 'CuotaDef', 'fk_cuota_def_id'),

			'fkConceptoCuota' => array(self::BELONGS_TO, 'CuotaConceptos', 'fk_concepto_cuota_id'),

		);

	}

        ...

}

And in the view action in the header controller I want to setup the data for Header and detail:


	public function actionView($id) {

		//$detalle = new CuotaDefDetalle('search');

		$CuotaDef = $this->loadModel($id, 'CuotaDef');

		$detalle = $CuotaDef->tCuotaDefDetalles;

		print_r($detalle);


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

			'model' => $CuotaDef, //$this->loadModel($id, 'CuotaDef'),

			'detalle'=> $detalle,

		));

	}



in the view I want to use CGridView widget:


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

	'id' => 'cuota-def-detalle-grid',

	'dataProvider' => $detalle,

	'columns' => array(

		'id',

		

		array(

				'name'=>'fk_concepto_cuota_id',

			//'title'=>'Concepto',

				'value'=>'GxHtml::valueEx($data->fkConceptoCuota)',

				

				),

		'importe',

	),

));

But I get an error that I cannot use this kind of object:


Fatal error: Call to a member function getData() on a non-object in C:\xampp\htdocs\yii\framework\zii\widgets\CBaseListView.php on line 105

I understand thet the widget needs a DataProvider object , how can I convert the line:

$detalle = $CuotaDef->tCuotaDefDetalles;

to a data provider object?

Best Regards

$detalle in your view is an array. So there is no way you can throw array to ‘dataProvider’ attribute in CGridView.

What you do in view instead:


public function actionView($id) {

                $CuotaDef = $this->loadModel($id, 'CuotaDef');

                //start here...

                $detalleModel = new CuotaDefDetalle('search');

                $detalleModel->fk_cuota_def_id = $id; //add manually the cuota_def_id to detalle's search criteria

                $detall = $detalleModel->search();

                //end here....


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

                        'model' => $CuotaDef, //$this->loadModel($id, 'CuotaDef'),

                        'detalle'=> $detalle,

                ));

        }

Also, depending on attribute data type, may need to add




$detalleModel->unsetAttributes();



right after instantiation.

/Tommy

Thank !

I’ll test it and let you know !

Untested by me but this is simpler and should perform better




public function actionView($id) {

  $CuotaDef = $this->loadModel($id, 'CuotaDef');


  $detalle = new CActiveDataProvider('CuotaDefDetalle', array(

    'criteria'=>array(

      'condition' => 'fk_cuota_def_id = '.$id

    )

  ));


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

    'model' => $CuotaDef, //$this->loadModel($id, 'CuotaDef'),

    'detalle'=> $detalle,

  ));

}



/Tommy