Cgridview Show Name Instead Of Id

Hi guys, I have a “user” table in which a user has as another user as boss, so I’m having a relation with the same table

User

id_user = 1

name = Jhon Boss

boss = null

id_user = 2

name = Peter Smith

boss = 1 <- This is the id of Jhon Boss

So when I create a CGridView I need to display within the boss column the name of the boss instead of the id, if it isn’t null.




'columns'=>array(

		'id_user',

		'name',

		'boss' // Here is where I need to display name instead of id



Thanks in advance!

Welcome to Yii forum!

Take a look at:

http://www.yiiframework.com/forum/index.php/topic/41584-relational-active-record-for-user-friend-relation/page__p__198146__hl__friend+relation#entry198146

http://www.yiiframework.com/forum/index.php/topic/32700-self-relations/page__p__157390__hl__+elf#entry157390

http://www.yiiframework.com/forum/index.php/topic/55338-how-to-setup-self-relation/page__p__253126__hl__+elf#entry253126

you could declare a association on User model boss like so


public function relations()

{

		return array(

			'myboss' => [self::BELONGS_TO, 'User', 'boss']

		);

}




// then in your gridview you can access it like so

'columns'=>array(

                'id_user',

                'name',

                'myboss.name'