How to display 2 models in 1 view (admin view)?

Hi All,

I want to add 2 (or more) models in 1 view (in admin view)…

I do it like it is described in this thread but have some problem…

When I click edit button for one record of second table it references the first table…

How come??

Please help

this is my controller:




	public function actionAdmin()

	{

		$model=new BeforeTasksFb('search');

		$model2=new BeforeTasksDsm('search');

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

		$model2->unsetAttributes();

		if(isset($_GET['BeforeTasksFb']) && isset ($_GET['BeforeTasksDsm'])){

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

			$model2->attributes=$_GET['BeforeTasksDsm'];

		}


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

			'model'=>$model,

			'model2'=>$model2

		));

	}



and admin.php





<?php

/* @var $this BeforeTasksFbController */

/* @var $model BeforeTasksFb */


Yii::app()->clientScript->registerScript('search', "

$('.search-button').click(function(){

	$('.search-form').toggle();

	return false;

});

$('.search-form form').submit(function(){

	$('#before-tasks-fb-grid').yiiGridView('update', {

		data: $(this).serialize()

	});

	return false;

});

");

?>


<h1>Manage Before Tasks Fbs</h1>


<p>

You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>

or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.

</p>




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

	'id'=>'before-tasks-fb-grid',

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

	'filter'=>$model,

	'columns'=>array(

		...

		array(

			'class'=>'CButtonColumn',

		),

	),

)); 


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

	'id'=>'before-tasks-dsm-grid',

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

	'filter'=>$model2,

	'columns'=>array(

		...

		array(

			'class'=>'CButtonColumn',

		),

	),

));?>



you can edit the button column like this:


'columns'=>array(

        array(

            'header' => 'Actions',

            'class' => 'CButtonColumn',

            'template' => '{view}&nbsp;{update}&nbsp;{delete}',

            'viewButtonImageUrl' => Yii::app()->baseUrl . '/images/gridview/' . 'gr-view.png',

            'updateButtonImageUrl' => Yii::app()->baseUrl . '/images/gridview/' . 'gr-update.png',

            'deleteButtonImageUrl' => Yii::app()->baseUrl . '/images/gridview/' . 'gr-delete.png',

            'buttons'=>array

    		(

		       'view' => array

		        (

		        	'url'=>'CController::createUrl("/blog/post/view", array("id"=>$data->id, "lang" => Yii::app()->getLanguage()))'

		        ),

		       'update' => array

		        (

		        	'url'=>'CController::createUrl("/blog/post/update", array("id"=>$data->id, "lang" => Yii::app()->getLanguage()))'

		        ),

		       'delete' => array

		        (

		        	'url'=>'CController::createUrl("/blog/post/delete", array("id"=>$data->id, "lang" => Yii::app()->getLanguage()))'

		        ),

 			),

         ),

	),

Thank you for your reply moginn.

I use your code and I get this error:

CController::createUrl() should not be called statically, asuming $this from incompatible context

Try this code




Yii::app()->createUrl("/blog/post/delete", array("id"=>$data->id, "lang" => Yii::app()->getLanguage()));



Yii::app()->createUrl() OR Yii::app()->createAbsoluteUrl()

Thank you very much mbala, it’s working now :)

May I ask why it was not working with CController::createUrl()?

CController::createUrl() is working fine outside the gridview

Ok, thanks :)

One last question:

It is working as it should now, but in this admin view, the 2 models are shown as 2 different tables.

Is there any chance I can show them is the same table?

I don’t know about your database design. If you can, try ‘CSqlDataProvider’ method to merge two tables