Get name instead of ID

Hello.

I followed larryullman’s tutorial (‘introduction to the yii framework’, it’s my first post, can’t use links) on building a new basic website with two related tables, Employees and Departments. When i am insering a new employee i already have a dropdown control to choose the department associated, the tutorial have an explanation on how to do that. Now i want to show the department name (instead of ID) in the list of employees, index page. What’s the correct way to do it?

Thank you.

Tutorial : www larryullman com/2009/06/18/introduction-to-the-yii-framework/

This looks simple, can U please pass your relations from model classes and code U are using in view. It should be easy to do it trough relations

1 Like

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.

Using the department id, you can use SQL JOIN to get department name. Go through the tutorial to find out how to use JOIN.