View Name in place of ID

HI friends

I just downloaded the YII and it is great framework ( I like GII tool)

i followed this

and I got the basic idea of YII (just half and hour ago I started working on YII)

Tutorial was awesome and i did all step correctly and my Employee application is working :lol:

but i have a problem in _view.php of employee

It is showing department ID in place of Department name

I tried to change this code but its not workin


<b><?php echo CHtml::encode($data->getAttributeLabel('departmentId')); ?>:</b>

	<?php echo CHtml::encode($data->departmentId); ?>

	<br />

help me so that in place of department ID it will show name

There’s, of course, a “Yii way” of handling that issue. First of all you got to make sure you have a defined a relationship with the Department model in your Employee model.

It should be something like this:




// protected/models/Employee


public function relations() {

        return array(

            'department' => array(self::BELONGS_TO, 'Department', 'departmentId'),

        );

    }



Now you can access the department from your employee view easily doing the following:




<?php echo CHtml::encode($data->department->name); ?>



You can read further on this HERE.

Thanks mauriciorivera

that’s what i want

have a nice day :)

You’re welcome, I’m glad it helped.