Yii Framework Forum: CDetailView and model relations - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CDetailView and model relations How to do it? Rate Topic: ***-- 1 Votes

#1 User is offline   vjbill 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 18-May 10
  • Location:Poland

Posted 12 June 2010 - 09:39 AM

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
0

#2 User is offline   lenye 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 04-February 10

Posted 12 June 2010 - 10:26 AM

$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:
0

#3 User is offline   vjbill 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 18-May 10
  • Location:Poland

Posted 12 June 2010 - 11:05 AM

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
1

#4 User is offline   lenye 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 04-February 10

Posted 12 June 2010 - 11:31 AM

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:
0

#5 User is offline   ortenheim 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 29-August 10

Posted 19 November 2010 - 06:12 AM

View Postlenye, on 12 June 2010 - 11:31 AM, said:

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?
0

#6 User is offline   Emanuel1989 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 23-November 10

Posted 28 November 2010 - 09:37 AM

View Postortenheim, on 19 November 2010 - 06:12 AM, said:

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
                 ),
        ),
)); 

1

#7 User is offline   Dezta 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 08-November 11
  • Location:Madiun

Posted 21 November 2011 - 05:11 AM

View Postlenye, on 12 June 2010 - 10:26 AM, said:

$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:


It's Work.. Thanks :rolleyes:
No Body can do Anything, But Everyone Can do Something
0

#8 User is offline   monk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 22-March 11

Posted 19 January 2012 - 07:08 AM

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

#9 User is offline   wyginwys 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 15-February 12

Posted 16 February 2012 - 06:06 AM

View Postortenheim, on 19 November 2010 - 06:12 AM, said:

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

Take a look at here
http://learnyii.blog...-cgridview.html

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

#10 User is offline   artificial 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 96
  • Joined: 11-December 10
  • Location:Sydney, Australia

Posted 16 February 2012 - 07:26 AM

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',
        ),
)); 

Alex Berriman
www.fishvision.com
0

#11 User is offline   wyginwys 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 15-February 12

Posted 17 February 2012 - 08:59 AM

View PostArtificial, on 16 February 2012 - 07:26 AM, said:

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users