1

Can I use entityQuery() to query entities in a Drupal 7 table?

I know it's possible to change the active database and then use a dynamic query to access the database, but can I do that also with entityQuery(), or does it only work with the default database?

1 Answer 1

1

The object returned from Drupal::entityQuery(), in Drupal 10, assumes the database has been created from Drupal 10. It will not work with a database created from Drupal 7 because Drupal 7 creates different tables which have different schemas.

With the object returned by Drupal::database(), you could query entities from a Drupal 7 database, but you would need to adjust the query for the schema used from Drupal 7.
Since you are directly querying the database table, that is not different from querying a database table that is not for entities, as long as you keep in mind the schema used for those tables in Drupal 7.

7
  • I'm not sure I understand. I think you are saying entityQuery() is not a good option for querying the Drupal 7 database from a Drupal 10 site. And that I'm better off using dynamic queries.
    – arnoldbird
    Aug 3, 2023 at 19:33
  • As long as you consider the schema used from Drupal 7, yes.
    – apaderno
    Aug 3, 2023 at 21:54
  • What does it mean to "consider the schema" when one is using entityQuery? Isn't the idea with database abstraction that you do not have to know what the tables look like? (I reversed some community edits to my question, because they obscure the purpose of the question. In a Drupal context, everyone knows what entityQuery is.)
    – arnoldbird
    Aug 4, 2023 at 15:04
  • 3
    The abstraction layer works with the code - so the DB abstraction layer for d8 knows how to use the d8 schema, but it doesn’t know how to use the d7 schema, which is different
    – Clive
    Aug 4, 2023 at 17:02
  • @arnoldbird When you use a class that implements \Drupal\Core\Entity\Query\QueryInterface to query the database to get entities, you cannot take in consideration the different schema, simply because you cannot set which database tables and which table rows it must query. My previous comment was referred to dynamic queries, which use different classes and which allow you to select which database table and which table rows to query.
    – apaderno
    Aug 4, 2023 at 17:56

Your Answer

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

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