Answer the question
In order to leave comments, you need to log in
What is the most rational way to get a collection of objects through two relationships?
Hello!
There is an entity (Object 1) to which several entities (Objects of type 2) are attached through a pivot table (many to many). In turn, several other entities (Type 3 Objects) are linked to Objects 2 through the pivot table.
What is the most rational and elegant way to get a collection of Objects of type 3 for Object 1.
Thank you!
Answer the question
In order to leave comments, you need to log in
stackoverflow.com/a/35316726/2456628
Before that, we do nested eager loading so as not to produce queries to the database.
Search for "chain links" through "object of the 2nd type" through reverse dependencies.
So, we do it in reverse order:
1. We take a collection of all 3 types of objects bound to the 2nd type.
2. From the result - we take all objects tied to the required ID of the 1st type.
Result: a collection of type 3 objects for the specified type 1 object.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question