Yii Framework Forum: A little more advanced relational query - Yii Framework Forum

Jump to content

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

A little more advanced relational query Rate Topic: -----

#1 User is offline   manilodisan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 17-September 09
  • Location:Bucharest

Posted 16 October 2009 - 11:38 PM

Let's say I have 3 tables related.

Groups -> (HAS_MANY) Labels -> (HAS_MANY) attributes

To get the groups with their labels I would use:
Model_name::model()->with ('labels')->findAll()


Is there a way to retrieve all records (groups+labels+attributes)?
0

#2 User is offline   manilodisan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 17-September 09
  • Location:Bucharest

Posted 18 October 2009 - 03:29 AM

Anyone on this?
0

#3 User is online   tri 

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

Posted 18 October 2009 - 05:37 AM

Have you tried this? Assuming an 'attribs' relationship declaration in Label.
Groups::model()->with ('labels', 'labels.attribs')->findAll()


For a complete list of the whole set you will need a nested foreach.
An alternative is to start the query from Attribs.
Attribs->(BELONGS_TO)Labels->(BELONGS_TO)Groups, that is.

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

#4 User is offline   manilodisan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 17-September 09
  • Location:Bucharest

Posted 18 October 2009 - 06:02 AM

View Posttri, on 18 October 2009 - 05:37 AM, said:

Have you tried this? Assuming an 'attribs' relationship declaration in Label.
Groups::model()->with ('labels', 'labels.attribs')->findAll()


Thanks Tommy, that did it.
0

#5 User is offline   Mike 

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

Posted 19 October 2009 - 03:07 AM

In that case it's also sufficient to write:

Groups::model()->with ('labels.attribs')->findAll()

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