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.
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.