Yii Framework Forum: Render A Second View Related To The Main One - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Render A Second View Related To The Main One Rate Topic: -----

#1 User is offline   JohnnyBeGood 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 11-October 12

Posted 23 October 2012 - 09:48 PM

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
0

#2 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 24 October 2012 - 01:15 AM

View PostJohnnyBeGood, on 23 October 2012 - 09:48 PM, said:

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,

		));

	}

0

#3 User is offline   JohnnyBeGood 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 11-October 12

Posted 24 October 2012 - 04:35 AM

View PostJimlam, on 24 October 2012 - 01:15 AM, said:

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).
0

#4 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 24 October 2012 - 08:32 AM

View PostJohnnyBeGood, on 24 October 2012 - 04:35 AM, said:

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
0

#5 User is offline   JohnnyBeGood 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 11-October 12

Posted 24 October 2012 - 02:36 PM

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
0

#6 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 25 October 2012 - 02:03 AM

View PostJohnnyBeGood, on 24 October 2012 - 02:36 PM, said:

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


0

#7 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 884
  • Joined: 20-April 11
  • Location:India

Posted 25 October 2012 - 02:58 AM

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..
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#8 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 884
  • Joined: 20-April 11
  • Location:India

Posted 25 October 2012 - 03:00 AM

u can render any view, give the path.

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

Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#9 User is offline   JohnnyBeGood 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 11-October 12

Posted 28 October 2012 - 07:34 PM

I got it.
Thanks everyone.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users