0

Note: this is my first Drupal project as I am usually working with Laravel.

I have some entities my-nodes that have a field EntityReferenceFieldItemList (a multi-select linking to other entities of another content type related-nodes).

I want to query the my-nodes entities together with the related entities in 1 query (in Laravel I would describe this as eager loading the relationship).

The my-nodes entities all reference different related-nodes entities and I need to access related-nodes related values.

I'd like to optimize the amount of calls to the database to possibly only 1.

Method referencedEntities does not look like what I need because it's lazy loading, not eager loading:

$node->get($field)->referencedEntities();

Any idea? Thanks in advance!

2
  • Laravel queries are pretty much the same as in Drupal. To eager load you join the tables and query the fields you are interested in. The method referencedEntities() is simply a method specific to the class mentioned, so only can be used after you've retrieved the parent node of the field, so it can only be lazy.
    – 4k4
    Sep 10 '17 at 22:00
  • Before you try to build a query for referenced entities, I would first build this in a View and see how Views puts together this query, because there are more than two tables involved when you have added the field to a content type (bundle field).
    – 4k4
    Sep 10 '17 at 22:25
3

This doesn't exist in Drupal, you can't load entities of different types at once, the best you can do is load all referenced entities of the same type at once, which is what referencedEntities() does.

Each entity type has its own storage handler which doesn't know anything about other entity types, as far as those handlers are concerned, those other entities might not even be in the database but stored in MongoDB or text files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.