Yii Framework Forum: Multiple relations - Yii Framework Forum

Jump to content

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

Multiple relations Rate Topic: -----

#1 User is offline   romanoza 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 117
  • Joined: 17-April 09
  • Location:Moscow, Russia

Posted 22 December 2009 - 09:54 AM

Hi, I have this code to get model with some relations:
$user = User::model()->with('trafficPartner.trafficSources.trafficSourceRequest')->findByPk(Yii::app()->user->id);

it's generate this SQL:
SELECT
 <bla bla bla>
FROM
  `User`
  LEFT OUTER JOIN
  `TrafficPartner` `trafficPartner` ON
    (`User`.`trafficPartnerId` = `trafficPartner`.`id`)
  LEFT OUTER JOIN
  `TrafficSource` `trafficSources` ON
    (`trafficSources`.`trafficPartnerId` = `trafficPartner`.`id`)
  LEFT OUTER JOIN
  `TrafficSourceRequest` `trafficSourceRequest` ON
    (`trafficSourceRequest`.`trafficSourceId` = `trafficSources`.`id`)
WHERE
  (`User`.`id` = 45)


and there is 3 records, but when i trying to get last relation:
var_dump($user->trafficPartner->trafficSources->trafficSourceRequest);


it' returns NULL

WTF?
I am not a wizard, i am just learning...
0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 22 December 2009 - 10:16 AM

View Postromanoza, on 22 December 2009 - 09:54 AM, said:

Hi, I have this code to get model with some relations:
$user = User::model()->with('trafficPartner.trafficSources.trafficSourceRequest')->findByPk(Yii::app()->user->id);

it's generate this SQL:
SELECT
 <bla bla bla>
FROM
  `User`
  LEFT OUTER JOIN
  `TrafficPartner` `trafficPartner` ON
    (`User`.`trafficPartnerId` = `trafficPartner`.`id`)
  LEFT OUTER JOIN
  `TrafficSource` `trafficSources` ON
    (`trafficSources`.`trafficPartnerId` = `trafficPartner`.`id`)
  LEFT OUTER JOIN
  `TrafficSourceRequest` `trafficSourceRequest` ON
    (`trafficSourceRequest`.`trafficSourceId` = `trafficSources`.`id`)
WHERE
  (`User`.`id` = 45)


and there is 3 records, but when i trying to get last relation:
var_dump($user->trafficPartner->trafficSources->trafficSourceRequest);


it' returns NULL

WTF?


trafficSources sounds like a possible array. Is it a HAS_MANY relationship?

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   romanoza 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 117
  • Joined: 17-April 09
  • Location:Moscow, Russia

Posted 22 December 2009 - 10:20 AM

View Posttri, on 22 December 2009 - 10:16 AM, said:

trafficSources sounds like a possible array. Is it a HAS_MANY relationship?

/Tommy


Yes, it's array but why can't I get collection of last relation without fetching pervious array of objects?
I am not a wizard, i am just learning...
0

#4 User is offline   romanoza 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 117
  • Joined: 17-April 09
  • Location:Moscow, Russia

Posted 22 December 2009 - 10:23 AM

Yeah, I forget this thing:
                      foreach($user->trafficPartner->trafficSources as $a) {
                            foreach($a->trafficSourceRequest as $B) {
                                echo $b->requestId.'-';
                            }
                        }

it's shows me that all is ok

but 2 foreach - bad
I am not a wizard, i am just learning...
0

#5 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 22 December 2009 - 10:26 AM

View Postromanoza, on 22 December 2009 - 10:23 AM, said:

Yeah, I forget this thing:
                      foreach($user->trafficPartner->trafficSources as $a) {
                            foreach($a->trafficSourceRequest as $B) {
                                echo $b->requestId.'-';
                            }
                        }

it's shows me that all is ok

but 2 foreach - bad


You might consider to start from trafficSourceRequest, using BELONGS_TO relationships.

(was applicable in my case)
/Tommy

This post has been edited by tri: 22 December 2009 - 10:27 AM

Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#6 User is offline   romanoza 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 117
  • Joined: 17-April 09
  • Location:Moscow, Russia

Posted 22 December 2009 - 11:23 AM

View Posttri, on 22 December 2009 - 10:26 AM, said:

You might consider to start from trafficSourceRequest, using BELONGS_TO relationships.

(was applicable in my case)
/Tommy


oh, good idea, thanks
I am not a wizard, i am just learning...
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