Allow DatabaseStatementInterface::fetch* methods to accept aliases rather than numerical keys

Created on 2 July 2011, about 13 years ago
Updated 18 June 2023, about 1 year ago

The following database fetch methods require numeric indices:

With dynamic queries, per Crell, there is "no guarantee" as to the order of the fields in the returned result. In particular, expressions are always added after fields, which can easily result in unexpected results being returned.

Consider the following example: One module defines a query with one field and one expression. The module expects the field to be index 0 and the expression to be index 1, and so uses:

$query->execute()->fetchAllKeyed(0, 1); // The default indices

However, another module alters the query, adding a field. The result? Suddenly, the first module is fetching an entirely different field!

It would be better for these methods to accept table aliases, like fetchAllAssoc() does. We could include a is_numeric() check for backwards compatibility and to allow the convenience of numeric indices for static queries, which are not affected by this issue.

I've updated the handbook with a warning about these methods at http://drupal.org/node/310075#executing_the_query. If this is fixed, that documentation can be removed.

πŸ“Œ Task
Status

Postponed: needs info

Version

9.5

Component
DatabaseΒ  β†’

Last updated less than a minute ago

  • Maintained by
  • πŸ‡³πŸ‡±Netherlands @daffie
Created by

πŸ‡ΊπŸ‡ΈUnited States xjm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024