CDetailView and model relations

Hi,

I’ve been playing around with a simple database with two tables: User and Profile. Profile has a foreign key, which points to the User table. Now what I wanted to achieve is a profile view, where I can display both data from the User and Profile tables.

I’ve noticed that there is this nice widget CDetailView, but the problem is with the attributes. E.g.




$this->widget('zii.widgets.CDetailView', array(

	'data'=>$profile,

	'attributes'=>array(

		'fname',

		'sname',

	),

)); 



displays the Profile model attributes fine (according to the attributeLabels() method). Now if I want to access the User table through the ActiveRecord relation, the attribute labels aren’t as they should be.




$this->widget('zii.widgets.CDetailView', array(

	'data'=>$profile,

	'attributes'=>array(

		'fname',

		'sname',

                'user.username'

	),

)); 



displays User Username instead of the attribute label configured in the User model.

I would appreciate any help with this. Or maybe I’m trying to do too much with that widget? Is there a better way? A simple HTML table with php displaying the values from models works, but I wanted something more elegant and flexible.

Thanks for your help,

Regards,

Bill




$this->widget('zii.widgets.CDetailView', array(

        'data'=>$profile,

        'attributes'=>array(

                'fname',

                'sname',

                array(

                   'lable'=>'username',

                   'value'=>'$data->user->username',

                 ),

        ),

)); 



try this and hope help you! :rolleyes:

Hi lenye,

Thanks for your help :). The ‘value’ property brought me to an idea, and now it works:




$this->widget('zii.widgets.CDetailView', array(

	'data' => $profile,

	'attributes'=>array(

		'fname',

		'sname',

		array(

			'label' => $profile->user->getAttributeLabel('username'),

			'value' => $profile->user->username

		)

	),

));



To clean up the code, I shall probably pass the User table model explicitly to the view, so I can have


$user->username

instead of


$profile->user->username

.

Regards,

Bill

Also, you can defined a method named getUsername() in your profile model,then you can get username by $date->username .




function getUsername(){

  return $this->user->username;

}



if you have to use username frequntly in profile, you can go this way. :rolleyes:

how can i make this text a link to the corresponding entry?

That code should help You:




$this->widget('zii.widgets.CDetailView', array(

        'data'=>$profile,

        'attributes'=>array(

                'fname',

                'sname',

                array(

                   'label'=>'username',

                   'type'=>'raw',   // here's a parameter that disable HTML escaping by Yii 

                   'value'=>'$data->user->username', // here You can simple use CHtml::link() method

                 ),

        ),

)); 



It’s Work… Thanks :rolleyes:

Pretty awesome. I have used this a couple of times now. Thanks

Take a look at here

BTW

I couldnt find a way of sending an array of two models as data to CDetailView and properly referencing them inside a view.

You should also be able to do it without creating an array, just by:


$this->widget('zii.widgets.CDetailView', array(

        'data'=>$profile,

        'attributes'=>array(

                'fname',

                'sname',

                'user.username',

        ),

)); 

This works thanks.