Render a view inside a view

I have two tables in my database, TableA and TableB. I used the Gii tool (which by the way is awesome) to create controllers, models and CRUD forms for the tables.

My problem is when i try to render TableA’s create form inside a CJuiDialog modal inside TableB’s view. The idea is to be able to create new entries of TableA without leaving the create form of TableB.

The code from _form.php of TableB that i added at the top is:


$this->beginWidget('zii.widgets.jui.CJuiDialog', array('id'=>'tableA')); // and some options too

$this->renderPartial('/TableA/create');

$this->endWidget('zii.widgets.jui.CJuiDialog');

This does not work and i get an error about ‘Undefined variable: model’ inside TableA/create.php.

I assume this is due to the $model variable inside the controller of TableA is not in scope. But how can i fix this?

pretty easy:




$this->renderPartial('...view...', array('model'=>$model));



To create multiple models with one view you would have to do something like this (assuming that the TableA model always has a TableB model)


TableAModelController.php


public function actionCreate()

	{


                $tableAModel=new tableAModel;

                $tableBModel=new tableBModel;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);

                if(isset($_POST['tableAModel']))

		{

			$tableAModel->attributes=$_POST['tableAModel'];

		}


                if(isset($_POST['tableBModel']))

		{

			$model->attributes=$_POST['tableBModel'];

		}


                $valid = $tableAmodel->validate();

                $valid = $tableAmodel->validate() && $valid;


                if($valid)

                {

                    $tableAModel->save();

                    $tableBModel->save();

                    $this->redirect(array('view','id'=>$tableAModel->id));

                }


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

			'model'=>$tableAModel,

                        'tableBModel'=>$tableBModel,

		));

	}

Then you would use:


$this->renderPartial('tableBModel/_create.', array('model'=>$tableBModel));