Answer the question
In order to leave comments, you need to log in
yii framework. Displaying linked tables as a list?
Hello everybody. There are 4 DB tables MySql
Users - tb users. With strings: country_id(country) region_id(region) city_id(City). Geo_countries - tb with countries (id, name, etc.).
Geo_regions - tb with regions (id, name, etc.).
Geo_city - TB with cities (id, name, etc.).
How in Yii1 to set the relationship of these tables, and display in the registration form so that, for example, choosing "Russia" would drop out only Russian regions?
Here is the code that I have already written:
The Users.php model file has a connection with the Geo_countries plate as follows:
/**
* @return array relational rules.
*/
public function relations()
{
return array(
'geo_countries' => array(
self::BELONGS_TO, 'GeoCountries', 'country_id'), );
}
<b><?php echo CHtml::encode($data->getAttributeLabel('country_id')); ?>:</b>
<?php echo CHtml::encode($data->geo_countries->name); ?>
<br />
<div class="row">
<?php $models = GeoCountries::model()->findAll(
array('order' => 'ordering'));
$list = CHtml::listData($models, 'id', 'name'); ?>
<?php echo $form->labelEx($model,'id', $data); ?>
<?php echo $form->dropDownList($model,'geo_countries', array('empty'=>'Выбор страны', $list)); ?>
<?php echo $form->error($model,'id'); ?>
</div>
>
Выбор страны
0
Страна 1
Страна 2
итд
Answer the question
In order to leave comments, you need to log in
Try this:
<?php echo $form->dropDownList($model, 'country_id', $list, array('empty' => 'Country select')); ?>
The second argument is the field of the User model, which is used to link with countries (you have it's country_id).
The third is a list of countries.
You set the connection of countries, regions and cities in Mysql by creating the fields accordingly:
country_id in Geo_regions
region_id in Geo_city
Next, you write these connections in the models of countries, regions and cities in relations ().
And about how to implement automatic loading of select values (interrelated selects) there is a lot of information in search engines.
UPD: well, be sure to write the rules for validating country_id, region_id, city_id in the models in the rules () so that the values \u200b\u200bare saved in the database.
I think it's better to use demos.krajee.com/widget-details/select2
to make ajax requests and load only what is needed. Otherwise, huge data arrays will turn out.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question