I would use your own code with just a few simple corrections! :]
[list=1][]Use code attribute in [] brackets or click second button to the right, when pasting a code, so it will be more readable here.[]I don’t think You have to declare $deptName =’’;[]You don’t have to check if $deptId is an empty string and again if it is NULL. I’m not sure, but I think you don’t have to check it at all - findByPk should handle properly situation when given parameter is NULL.[]Why you are returning deptNameObj instead of department name?[/list]I would change your code into
public function getDeptName()
{
if($deptId != NULL) return Dept::model()->findByPk($this->dept_Id, '', array('select'=>'strDeptName'))->strDeptName;
}
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'Dep' => array(self::BELONGS_TO, 'Dep', 'Dep_id'),
);
}