Render A Second View Related To The Main One

Hello Everyone

Could you please help me to manage the following?

I have two models: company and employee.

(the user table has a foreign key refering the company id)

I would like to show, when an user goes to view the company under the ‘company/x’ url, a second view with a list of all the employees that belong to the company x.

How can I achieve it?

Many thanks

You can add a link in one of the views of company which will bring you to a view of the employees of that company.

For example, you can modify the view.php file of your company by adding a link thus:-




$this->menu=array(


	array('label'=>'List Company', 'url'=>array('index')),


	array('label'=>'Create Company', 'url'=>array('create')),


	array('label'=>'Update Company', 'url'=>array('update', 'id'=>$model->id)),


	array('label'=>'Delete Company', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),


	array('label'=>'Manage Company', 'url'=>array('admin')),

	array('label'=>'View Employees', 'url'=>array('employee/index','id'=>$model->id)), // added line: assuming your company pk is id	


);


?>



You must then modify the actionIndex of your employee controller in this way :





	public function actionIndex($id=null)


	{

		$dataProvider=new CActiveDataProvider('Employee');		

		if($id != null)  


		$dataProvider=new CActiveDataProvider('Employee',array(

			'criteria'=>(array(

				'condition'=>"companyId=$id", // if companyId is FK

				)

			)

			));


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


			'dataProvider'=>$dataProvider,


		));


	}



Jimlam, thanks for the quick solution provided.

However I’ve asked about having 2 views on the same page (not under different pages).

Sorry, I misunderstood your question. I imagine you could, for example, add in a view of your company (view.php) the following lines:-




<?php		$dataProvider=new CActiveDataProvider('Employee',array(

			'criteria'=>(array(

				'condition'=>"companyId=$model->id",

				)

			)

			)); ?>

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


	'dataProvider'=>$dataProvider,


	'itemView'=>'../employee/_view',


)); ?>




I took here one of the existing views of employee. But I think the result could not be too good looking. You can work out a view of your own which will look better

Hi Jimlam,

This is exactly what I was looking for. There is only one issue. The Employee ID link is pointing to Company/x instead of Employee/x.

Many thanks once again

Modify your employee view file (_view.php) like this:




<?php echo CHtml::link(CHtml::encode($data->id), array('/employee/view', 'id'=>$data->id)); ?>



Another small change because I found it was not necessary to include the ‘…’ for the path:




<?php $this->widget('zii.widgets.CListView', array(        'dataProvider'=>$dataProvider,

        'itemView'=>'/employee/_view',)); ?> // remove '..' not necessary




u can render any number of views to a view…use renderPartial

eg;-


echo $this->renderPartial('ur-view-file-name', array('model'=>$model,'val'=>$_GET['val']));

in this example u will $model,$val in the view…

u can render any view, give the path.


echo $this->renderPartial('/employee/_view', array());

I got it.

Thanks everyone.