Yii Framework Forum: Struggling with RELATIONS - Yii Framework Forum

Jump to content

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

Struggling with RELATIONS Rate Topic: ***-- 1 Votes

#1 User is offline   amc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 13-March 09

Posted 30 December 2009 - 01:55 PM

Can anyone suggest how to solve the following

I have the following relations

company  (id, ...)  
region   (id, company_id, ...) 
division (id, region_id, ...)
user     (id, division_id, ...)


A company has many regions, a region has many divisions and a division has many users.

Given a user, I want to find the company the user belongs to, so a simple sql query to find this would be

select company.* from company, region, division, user where company.id = region.company_id 
AND region.id = division.region_id AND division.id = user.division_id AND user.id = ?????;


HOWEVER what I need to know is:

Is it possible to define a relation in the User model to retrieve the related company.

Thus I can simply perform a query such as
 User::model()->findByPk(1)->company


If its not possible to define this with a relation could it be done with a model method?

Any help would be appreciated.

amc
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 30 December 2009 - 02:21 PM

Configure the relations in the respective models like this:

user BELONGS_TO division
division BELONGS_TO region
region BELONGS_TO company


See the guide on AR relations for details.

Then you can fetch your data like this:

$user=User::model()->with('region.division.company')->findByPk(1);
if ($user!==null)
  $company=$user->region->division->company;

0

#3 User is offline   amc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 13-March 09

Posted 30 December 2009 - 04:07 PM

View PostMike, on 30 December 2009 - 02:21 PM, said:

Configure the relations in the respective models like this:

user BELONGS_TO division
division BELONGS_TO region
region BELONGS_TO company


See the guide on AR relations for details.

Then you can fetch your data like this:

$user=User::model()->with('region.division.company')->findByPk(1);
if ($user!==null)
  $company=$user->region->division->company;



Thanks Mike,

Got it to work by simply defining a method in the User model as follows, but really wanted to know if it could be done via a relation.

        public function getCompany()
        {
           return Company::model()->findBySql(
                           'SELECT * from company,region,division,user 
                            WHERE company.id = region.company_id 
                            AND region.id = division.region_id 
                            AND division.id = user.division_id 
                            AND user.id = :id',array(':id'=>$this->id)
           );
        }


PS. In your example the order of the with tables doesn't work as user doesn't have a region relation. So needs to be
user=User::model()->with('division.region.company')->findByPk(1);


amc
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