Department Model
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'employees' => array(self::HAS_MANY, 'Employee', 'departmentId'),
);
}
Employee Model
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'department' => array(self::BELONGS_TO, 'Department', 'departmentId'),
);
}
Employee view.php
<?php
/* @var $this EmployeeController */
/* @var $model Employee */
$this->breadcrumbs=array(
'Employees'=>array('index'),
$model->id,
);
$this->menu=array(
array('label'=>'List Employee', 'url'=>array('index')),
array('label'=>'Create Employee', 'url'=>array('create')),
array('label'=>'Update Employee', 'url'=>array('update', 'id'=>$model->id)),
array('label'=>'Delete Employee', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
array('label'=>'Manage Employee', 'url'=>array('admin')),
);
?>
<h1>View Employee #<?php echo $model->id; ?></h1>
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'departmentId',
'firstName',
'lastName',
'email',
'ext',
'hireDate',
'leaveDate',
),
)); ?>
Employee _view.php
<?php
/* @var $this EmployeeController */
/* @var $data Employee */
?>
<div class="view">
<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('departmentId')); ?>:</b>
<?php echo CHtml::encode($data->departmentId); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('firstName')); ?>:</b>
<?php echo CHtml::encode($data->firstName); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('lastName')); ?>:</b>
<?php echo CHtml::encode($data->lastName); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('email')); ?>:</b>
<?php echo CHtml::encode($data->email); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('ext')); ?>:</b>
<?php echo CHtml::encode($data->ext); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('hireDate')); ?>:</b>
<?php echo CHtml::encode($data->hireDate); ?>
<br />
<?php /*
<b><?php echo CHtml::encode($data->getAttributeLabel('leaveDate')); ?>:</b>
<?php echo CHtml::encode($data->leaveDate); ?>
<br />
*/ ?>
</div>
It should be easy, but i’m new to yii framework and i can’t figure it out. I want to show the Department name in the 2 views, instead of the Department ID.
Thanks for your help.