Score:1

Can I use entityQuery() in a Drupal 10 site to query entities from a Drupal 7 database?

br flag

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?

Score:1
us flag

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.

br flag
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.
apaderno avatar
us flag
As long as you consider the schema used from Drupal 7, yes.
br flag
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.)
cn flag
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
apaderno avatar
us flag
@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 avatar
us flag
To make a practical example with a function that queries the database to get entity data, even if it does not use any class that implements `\Drupal\Core\Entity\Query\QueryInterface`: If you use the Drupal 10 [`_tracker_add()`](https://api.drupal.org/api/drupal/core%21modules%21tracker%21tracker.module/function/_tracker_add/10) function on a Drupal 7 database, it will not work. In fact, the Drupal 7 [`_tracker_add()`](https://api.drupal.org/api/drupal/modules%21tracker%21tracker.module/function/_tracker_add/7.x) function uses a different query.
apaderno avatar
us flag
The used query is `'SELECT nid, status, uid, changed FROM {node} WHERE nid = :nid'` in Drupal 7 and `'SELECT [nid], [status], [uid], [changed] FROM {node_field_data} WHERE [nid] = :nid AND [default_langcode] = 1 ORDER BY [changed] DESC, [status] DESC'` in Drupal 10.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.