0

I created some custom entity types, let's call them "parent_entity_type" and "historic_data_entity_type". The parent entity type is referencing to historic_data_entity_type indicating which data is currently up to date and historic_data_entity_type has a field referencing to the parent for a full historic overview.

I want to have a parent entity always be fully loaded with data from referenced historic entity as soon as possible during the entity loading process. Therefore I would prefer to do it in my custom entity class or somewhere at this level instead of using hook_entity_load or similar hooks which are called later in the process.

Is there somebody who can give me a hint how to achive that? Looking at Entity::load() I feel that I might need to setup some sort of custom EntityStorage.

  • 4
    An entity reference field already does provide a computed property entity which loads the referenced entity automatically. Or is this question about performance, for example trying to load all data in one sql query? – 4k4 Nov 25 '18 at 12:16
  • It is about the last - I would like to assure that during loading the parent entity the child entity is always loaded as well for early caching of all the data. Or do I think to complicated - maybe this would be some kind of over engineering? – Tobias Krause Nov 26 '18 at 9:25
  • 1
    Probably, because entity data is cached already in memory. So it shouldn't make a difference which part of your code loads the entity first, for all subsequent loads the same instance is returned. Performance is more an issue for edge cases loading big amount of entity field data. – 4k4 Nov 26 '18 at 10:36
  • Thank you for that feedback. I will simply use the computed property entity for the referenced child entity – Tobias Krause Nov 26 '18 at 18:03

Your Answer

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

Browse other questions tagged or ask your own question.