(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.
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,
));
}
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