Yii Framework Forum: Display name instead id in view - Yii Framework Forum

Jump to content

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

Display name instead id in view Rate Topic: ****- 1 Votes

#1 User is offline   Inori 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 17-July 10

Posted 17 July 2010 - 01:05 AM

Hello,

probably a very newbish question, but I've literally just started using Yii and frameworks in general and am a bit lost.

Basically let's say I have Country table (id, name) and city table (id, country_id, name). I made city.country_id associate with country.id via FK comment thing.

Now, when I load up city view right now it shows:

Id - ..
Country_id - ..
Name - ...

What I want is to change country_id into country.name
How is the best way to do that?

Thanks in advance!
0

#2 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 618
  • Joined: 08-May 09

Posted 17 July 2010 - 04:32 AM

if your city has a relation country (BELONGS_TO), then:

$model = City::model()->findByPk($cityId);
echo $model->country->name;


http://www.yiiframew...de/database.arr
0

#3 User is offline   Inori 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 17-July 10

Posted 17 July 2010 - 04:54 AM

Not exactly the solution I was looking for, but it pointed me in the right direction, thanks!

In case anybody else will be in my boat, what you will need to do is edit view.php in view/modelname/view.php
<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(
                'id',
		'name',
                'country_id')); ?>

into
<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(
                'id',
		'name',
                 array(
                    'label' => $model->country->getAttributeLabel('country'),
                    'value' => $model->country->name
                ),)); ?>

0

#4 User is offline   smug 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 29-April 11

Posted 08 June 2011 - 05:41 AM

View PostInori, on 17 July 2010 - 04:54 AM, said:

Not exactly the solution I was looking for, but it pointed me in the right direction, thanks!

In case anybody else will be in my boat, what you will need to do is edit view.php in view/modelname/view.php
<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(
                'id',
		'name',
                'country_id')); ?>

into
<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(
                'id',
		'name',
                 array(
                    'label' => $model->country->getAttributeLabel('country'),
                    'value' => $model->country->name
                ),)); ?>


hi
but when i try this it says country.country is not defined
0

#5 User is offline   Sadaf 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 15-July 10
  • Location:Islamabad, Pakistan

Posted 08 June 2011 - 06:20 AM

That is because you don't have a relation named country in your model class. Either define a relation in relations method or change country to whatever is the name of your relation.


View Postnalajala, on 08 June 2011 - 05:41 AM, said:

hi
but when i try this it says country.country is not defined

0

#6 User is offline   Gajanan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 30-March 13
  • Location:Pune

Posted 19 December 2013 - 05:16 AM

Hi,

You need to set relation in model.php

return array(

'country'=>array(self::BELONGS_TO, 'Country', 'country_id'),
);


Now in admin.php

array(
'name' => 'country_id',
'header'=>'Counrty Name',
'value' => '$data->country->name'
),
0

#7 User is offline   juanfersanchezv 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 28-March 17

Posted 28 March 2017 - 01:07 PM

Hi newbie,

------ in your model -------

you always have a public function that contains all the relations

public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'idCounty' => array(self::BELONGS_TO, 'name_of_your_model that you are going to use', 'id_country'),

);
}

-----in your index view ---------
note: i use yii booster but, the function is very similar

<?php $this->widget('booster.widgets.TbDetailView', array(
'data'=>$model,
'attributes'=>array(

array ('name'=>'id_contry',
'value'=>$model->idCounty->name of the column you need to display,
'type'=>'text',
),

),
)); ?>

hope solve your problem with this solution
0

#8 User is offline   Medical Center Dubai 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 16-March 17
  • Location:Jumierah Beach Road, Umm Suqeim 1 Dubai-UAE

Posted 20 April 2017 - 05:59 AM

You have truly been an inspiration.
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