Let's say I have two custom entity types: entity_one, entity_many.

I know how to use an EntityQuery to find entity_one matches if there is a multi-value entity reference field on entity_one.

$results = \Drupal::entityTypeManager()->getStorage('entity_one`)
    ->condition('field_referencing_many.entity:entity_many.field_string', $value)

but what if there was no entity_one.field_referencing_many and instead there was a entity_many.field_reference_one that allows each entity_many child entity to reference its parent? Is there any magic in EntityQuery that would allow the tables to be joined and queried with that reverse entity_reference field? I still want results from entity_one storage.

  • Simply query the field directly in that case ->condition('field_referencing_one', $value) where $value is the entity ID you want references to. Nov 10, 2023 at 16:39
  • I don't know the entity_one IDs that I want. That is the point of the query :) I also don't know the entity_many IDs. All I know is entity_many.field_string has a value of $value. (this is obviously a made up example)
    – JonMcL
    Nov 10, 2023 at 17:39
  • OK so you want to filter on that key and get the related entities (rather than the original entities). I would think this is probably easier to achieve using a SELECT query rather than an entity query, but that doesn't answer your question so hopefully somebody else can, Nov 11, 2023 at 18:28


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.