Nested Relations

Hey guys

How can I use grid and filter in that condition:

are 4 tables, country , state , city and user

country(id, country_name)

state (id, country_id, state_name)

city (id, state_id, city_name)

user (id, city_id, fields…)

Country model relations

return array(

        'states' => array(self::HAS_MANY, 'State', 'country_id'),


    );

State relations

return array(

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


        'cities' => array(self::HAS_MANY, 'City', 'state_id'),


    );

City relations

return array(

        'states' => array(self::BELONGS_TO, 'State', 'state_id'),


        'users' => array(self::HAS_MANY, 'User', 'city_id'),


    );

User relations

return array(

    'cities' => array(self::BELONGS_TO, 'City', 'city_id'),

);

How can I bring on User grid, the fields country and state also, and how can I apply filters? I want to filters users by country… or filter by state … or in City grid filter by country also…

anyone know how if is possible using CGridView?

a simple equivalent query:

SELECT fields FROM user

JOIN city ON city.id=user.city_id

JOIN state ON state.id=city.state_id

JOIN country on country.id=state.country_id

Hi,

you can write a code something look like this…on city grid view display the country name


  array(

            'header' => 'Country',

            'name' => 'country_id',

            'value' => '$data->country_name',

            'filter' => GxHtml::listDataEx(Country::model()->findAllAttributes(null, true)),

            'htmlOptions' => array('width' => '15%'),

        //'filter'=>GxHtml::listDataEx(VenueCategoriesDetail::model()->findAll("status='1'"), 'venue_category_id', 'categories_name'),

        ),

and then after you can write a query on model Search function.


if (isset($this->country_id) && !empty($this->country_id)) {

			$criteria = new CDbCriteria;

			$criteria->select = 't.*, tu.* ';

			$criteria->join = ' LEFT JOIN `city` AS `tu` ON t.id= tu.country_id';

			$criteria->addCondition("country_name='" . $this->country_id. "'");

		}