Score:1

How to query on last delta?

ai flag

The documentation for QueryInterface::condition explains how to narrow a condition to a specific delta of the field being queried. Is there a way to tell the interface to look at the value for the last delta of the field? I tried providing -1 for the delta, as this method is used by many programming languages for access to array elements counting from the end, but that only resulted in empty results sets. Am I going to have to resort to either making up a dummy field to store the last value for the field separately and write some additional code to make sure that field always gets populated/updated, or giving up on the entity query interface and use SQL directly instead?

ru flag
You could try to sort descending and limit the result to 1
ai flag
@Hudri I know about applying a sort to the results from the complete query. And I know about limiting those results (again, for the complete query). Can you explain how to apply those techniques to individual conditions _within_ the entity query?
ru flag
Uhm, good question, you got me. Sorting and limiting will work with [Database API](https://www.drupal.org/docs/8/api/database-api/dynamic-queries/introduction-to-dynamic-queries), but not with EntityQuery directly. But there was a somewhat similar question a few days ago, [you could add a "tag" to an entity query](https://drupal.stackexchange.com/questions/307285/what-method-should-i-use-instead-of-drupal-core-entity-query-sql-queryaddexpre), and use that tag to add Database API expressions to an EntityQuery.
ai flag
Thanks, @Hudri. I have no doubt that would work. I get the impression, though, that the Drupalistas frown on using SQL directly on the underlying database tables for entities, on the grounds that there is no documented guarantee that the table/column naming conventions will never change, making that approach fragile. I'm inclined to think that if I'm going to ignore that advice I might as well just use the Database API for the entire query. I can do that in my sleep, and I would get much more flexibility and efficiency. In for a penny, in for a pound, right?
ru flag
Well, just like EntityQuery database API does handle table name prefixes, and AFAIK this is the only thing allowed to change in between installations or environments. Anything else would be a breaking change requiring a deprecation notice first and finally a bump in the major version number. This is the purpose of semantic versioning. And if that unlikely change ever happens, it most likely will brake both, EntityQuery and Database API.
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.