I am unable to eagerly load the polymorphic parent of a child class. The include statement seems to make no difference.
Child Class:
@BelongsToPolymorphic(
parents = {ParentRequest.class},
typeLabels = {"parent_request"})
public class Child extends Model {
}
Parent Class:
public class ParentRequest extends Model {
}
Query that should eagerly return child + parent:
List<Child> children = Child.where("... limit 500").include(ParentRequest.class);
children.size(); //this gives me 500
children.cachedParents.size(); //this gives me 0;
Ultimately, I am trying to speed up the following operation:
for (Child child : children) {
ParentRequest pr = child.parent();
// lots of pr.getString("parent_field");
...
}
I have benched these operations, and the above operation seems to take around 67 ms regardless of whether .include(ParentRequest.class) is used on the Child.where() method or not.
Any insight or help is greatly appreciated.
NOTE: I am aware the Child only has one parent. In the near future it will have several.
EDIT: Inverting the Query produced much faster results for some reasons. That is, rather than looking for Children and including ParentRequest, if I searched for ParentRequest and included Child the operation was much faster. Note that I specifically did a findBySql to join the child table to the parent_request table in my results. Below I've left in the specifics of the query.
List<ParentRequest> parents = ParentRequest.findBySQL("SELECT child.*, parent_requests.* " +
"FROM child JOIN parent_requests ON child.parent_id=parent_requests.id WHERE " +
"RAND()<=? AND (child.metersToA BETWEEN ? AND ?) " +
" AND (child.metersToB BETWEEN ? AND ?) limit ?",
decimation_value,
minDistanceToA, maxDistanceToA ,
minDistanceToB, maxDistanceToB,
MAX_POINTS).include(Child.class);