DropdownList value in View

Hi, I have recently used Yii2 and I have a question about Dropdownlists: I load a Dropdownlist from a Nations table of a DB (id_nation, nation), I see the list of countries, I select Spain, Salvo and in the View I see the id_nation.
How do I see the nation value?
Are there already posts with this solution?

Thank you

Do you have a relationship between your tables?

Yes :: (Nations) id_nazione --> p_nazione (STATES_REGIONS) and it’s called ‘stati_regioni_ibfk_1’

Anyone answer me ?? Thank you

Hi, please post here your code which renders DropdownList and Model code from which you get data for DropdownList items.

Table Nations


Table Patients



Relation 1 --> oo

id_country --> p_country_residence

In the view\patient_form.php

<?= $form->field($model, 'p_country_residence')->dropdownlist( ArrayHelper::map(nations::find()->all(),'id_country','country'), 'prompt'=>'Select the country of residence ...']) ?>

In the view\patient\view.php

<?= DetailView::widget([ ... 'attributes' => [ 'id_patient', ... 'p_country_residence', //I want to show Italy and not 43 (= id_country) ... ], ]) ?>

You will need to add new getter method to your Patient model.
For example:

public function getCountryName()
    if ($this->p_country_residence) { return $this->nations->country; }

And then in your DetailView, replace p_country_residence with countryName

This solution will work only if you have relations between Patient and Nations models setup correctly. Were your models generated by Gii? Has your Patient model some method like the one below?

public function getNations()
    return $this->hasOne(Nations::className(), ['id_country' => 'p_country_residence']);

Thanks, it works perfectly :slight_smile:

I exulted too early … it only works for p_country_residence.
I have a second field in the Patients table, p_country_birth, related to id_country: id_country (one) -> p_country_birth (many).
With your solution it always gives me the same country !!!

I was not aware that you have 2 foreign keys in the table Patients pointing to the same table and column. In this situation we need to replace getNations method in the Patients model by two separate getter methods. It is because these methods define relationships between models. And in my original solutions there is a condition which says that every time when we call attribute nations in Patients model we get a Nations model instance with id_country === p_country_residence

Patients model

public function getCountryBirth()
    return $this->hasOne(Nations::className(), ['id_country' => 'p_country_birth']);

public function getCountryResidence()
    return $this->hasOne(Nations::className(), ['id_country' => 'p_country_residence']);

public function getCountryBirthName()
    if ($this->p_country_birth) { return $this->countryBirth->country; }

public function getCountryResidenceName()
    if ($this->p_country_residence) { return $this->countryResidence->country; }

And then in your DetailView change attributes to countryResidenceName and countryBirthName

thank you so much for your help.
It works !!

Hi I have another problem,
I’m in SignupForm and I want to duplicate the default image in the frontend \ web \ avatars \ anonymous.jpg folder always in the same folder but with the name $ user-> username.jpg.
I tried several codes, but I can’t and it is frustrating because with the classic Php it is immediate with the copy function.
Thank you

Hi, you should create a new thread for this problem and also try to describe more specifically with what are you struggling. Do you have a problem with the copy() function? If that’s the case make sure you are using an absolute filesystem path. For example:

$dir = '/var/www/yii2project/web/avatars/';
/* or if you're using windows */
$dir = 'C:\\path\\to\\your\\project\\web\\avatars\\' ;

You will need to add new getter method to your Patient model. For example:

`public function getCountryName()


return $this->nations->country;

} `

This solution would work only if you have relations between models setup correctly. Were your models generated by Gii?

Dňa ut, 14. apr 2020, 16:06 bosone via Yii Forum forum@yiiframework.com napísal(a):