Relation Depth with LoadRelationsQueryBuilder

We have successfully implemented retrieving more than the default 10 related objects by using the LoadRelationsQueryBuilder. However, are not sure how to go about pulling the related objects FOR those related objects. Have tried using .setRelationDepth(1) but nothing is returned.

Is there documentation that outlines how to get related objects or set relation depth when using LoadRelationsQueryBuilder?

Thanks in advance.

Relation depth is used when you load primary objects and need to get the related objects at the same time. However, in that case, the size of the related collections is limited to 10 related objects.

When you specifically load relations, the API retrieves the immediately available related objects (get children for a parent). If you need to retrieve “grandchildren”, you’d still use the “get children for a parent” API.


Makes sense.

So I assume that would mean taking the original returned Promise of related children and iterating through each child object to get the id and use that in a subsequent LoadRelationsQueryBuilder where the parentID is the child’s id to get the grandchild? Might be over-complicating what you meant.

With that being said, would it not just be easier to perform a call directly to the child’s table with a WHERE clause that pairs it down to only the related ones then use .setRelationDepth there? At that point the relations are 1-1 so the default of 10 being returned is fine.

Could you please clarify what you meant by the following:?

To give an arbitrary example: I have a calendar day with calendar entries (for that day).

Typically I could get the calendar entries by getting the calendar day and setting a relation depth of 1 to get the entries. If I wanted to get the activities associated with each entry I could use relation depth of 2. Had to stop using this as I would only get the default 10 calendar entries back for each calendar day and needed more. So I switched to using LoadRelationsQueryBuilder and now I can pull up to 100 (which is great).

But now that I am using LoadRelationsQueryBuilder, I cannot pull the activities for each calendar entry after trying setRelationDepth.

What I meant with “…a WHERE clause that pairs it down to only the related ones…” is that I could go directly to the CalendarEntry table and use a where clause such as ‘WHERE = certain id’ to get only the entries that match the id of the parent object. We have a separate column that links the id of the calendar day to each entry. And from there if I set a relation depth of 1 I could get the exact activity details I am looking for all in one object. This mainly plays off the caveat that we have a column that tells you the id of the parent for each child tuple.

I am just not sure which way is easier, the way you suggested seems to require a lot of async calls (using the id of each child to then find the data for any relation columns). Again, I could be misunderstanding your solution

I think going directly to CalendarEntry and retrieving those objects as the primary ones is the way to go. In fact, using that approach, you can add properties which would include some data from both parent and child tables.

Okay, thanks for the help. I will try that out then.

I am pretty sure I got it working using a much quicker way.

I was scoping around some of the functions attached to DataQueryBuilder and it seems that .setRelationsPageSize() does exactly what I needed. Can now combine page size with relation depth for children of the original parent so that all fields are returned in the single call.

1 Like