Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » How do I get all objects indirectly referred to by another?

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

Page: 1
Go to End
Author Topic: 924 Views
  • HansR
    Avatar
    Community Member
    140 Posts

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

    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.

    Hans

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: How do I get all objects indirectly referred to by another? Link to this post

    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.

    924 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.