Yii Framework Forum: Why not BELONGS_TO through? - Yii Framework Forum

Jump to content

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

Why not BELONGS_TO through? How to build a relation between City and Country through the Region? Rate Topic: -----

#1 User is offline   igorsantos07 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 29-May 10
  • Location:Rio de Janeiro, Brazil

Posted 29 March 2012 - 01:01 PM

My database was modeled like this (reducing tables into its keys):

City
- id
- region_id
Region
- id
- country_id
Country
- id


I would like to do something like that:
$city = City::model()->with('region', 'country')->findByPk(1);
echo "$city - {$city->region->acronym}, {$city->country->name}";


But I can't figure out how to build the 'country' relation, since it would be a BELONGS_TO with 'through' => 'region' option.

Any help or idea would be greatly appreciated.
0

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 29 March 2012 - 04:17 PM

I haven't tried it yet, but give it a try
$city = City::model()->with(array('region' => array('with' => 'country')))->findByPk(1);


Inspired from:
http://www.yiiframew...eria-in-search/

And maybe test with simple BELONGS_TO relations (city/region and region/country), and use
echo "$city - {$city->region->acronym}, {$city->region->country->name}";

0

#3 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 799
  • Joined: 02-July 10
  • Location:Central Poland

Posted 18 April 2012 - 11:19 AM

also:
$city = City::model()->with(array('region', 'region.country'))->findByPk(1);

should work
red
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