Render A Second View Related To The Main One
#1
Posted 23 October 2012 - 09:48 PM
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
#2
Posted 24 October 2012 - 01:15 AM
JohnnyBeGood, on 23 October 2012 - 09:48 PM, said:
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,
));
}
#3
Posted 24 October 2012 - 04:35 AM
Jimlam, on 24 October 2012 - 01:15 AM, said:
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).
#4
Posted 24 October 2012 - 08:32 AM
JohnnyBeGood, on 24 October 2012 - 04:35 AM, said:
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
#5
Posted 24 October 2012 - 02:36 PM
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
#6
Posted 25 October 2012 - 02:03 AM
JohnnyBeGood, on 24 October 2012 - 02:36 PM, said:
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
#7
Posted 25 October 2012 - 02:58 AM
eg;-
echo $this->renderPartial('ur-view-file-name', array('model'=>$model,'val'=>$_GET['val']));in this example u will $model,$val in the view..
Wiwo inc.
| Mobile: 919995504508
#8
Posted 25 October 2012 - 03:00 AM
echo $this->renderPartial('/employee/_view', array());
Wiwo inc.
| Mobile: 919995504508

Help














