Yii Framework Forum: Nested Relations - Yii Framework Forum

Jump to content

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

Nested Relations Rate Topic: -----

#1 User is offline   MarcosX 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 21-July 09

Posted 13 July 2013 - 11:19 AM

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
0

#2 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 840
  • Joined: 19-February 13

Posted 14 July 2013 - 11:46 PM

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

Thanks,
Ankit Modi
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