V
V
Vasily the Dark2016-11-15 11:07:52
MySQL
Vasily the Dark, 2016-11-15 11:07:52

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

I display the country in the user profile like this (views.php file):
<b><?php echo CHtml::encode($data->getAttributeLabel('country_id')); ?>:</b>
<?php echo CHtml::encode($data->geo_countries->name); ?>
<br />

In the user edit and create form file, I display a drop-down list like this:
<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>

I didn't touch anything in the controllers. Neither in userController nor in geoCountriesController.
The list is displayed but somehow strange ... after the "Choice of the country" some kind of 0 (zero) incomprehensible got out.
This is what it looks like:
>
Выбор страны
0
Страна 1
Страна 2
итд

Some kind of zero that came out of nowhere came out, and most importantly, when editing or creating a user, country data is not recorded in the database. That bish is not saved.
I really need help from yii1 experts. What did I do wrong? Why is nothing written to the database... and what is this strange zero in the array?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kuzmina Maria, 2016-11-15
@orriole

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.

M
Maxim Timofeev, 2017-02-08
@webinar

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 question

Ask a Question

731 491 924 answers to any question