Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How do I get all objects indirectly referred to by another?

Go to End

2 Posts   1316 Views


Community Member, 141 Posts

26 May 2011 at 9:48am

I have a collection of object classes with the following relations:
- Class 1 has many objects of Class 2
- Class 2 has many objects of Class 3
- Class 3 is just another data class

That is, a single Class 1 object has multiple Class 2 objects which in turn have multiple Class 3 objects.

How would I execute a query to get all objects of Class 3 that are indirectly referred to by Class 1? Can this be done efficiently? Or do I need to add a Class 1 has_one relation to class 3 (something which sounds ugly and would prefer to avoid)? This is an operation that might be done often, so it needs to be efficient.



Forum Moderator, 5523 Posts

26 May 2011 at 8:02pm

Well you can't get all the Class 3 objects indirectly without getting the other classes. You'll need the id's. It depends on your dataset size, using the ORM (so ClassOne->Relationship()->Relationship() will return what you want but would be slow.

You could also use DataObject::get with a join clause (get Class 3, join on class 2) which would be pretty quick. I suggest you try with a simply join operation with DataObject get and see how performance goes. Cache if required. Next level in optimization is to denormalize your data, thus removing the need for joins.