ConditionInterface::condition() should be stricter about needing a $value in certain situations

Created on 19 July 2018, over 6 years ago
Updated 17 February 2023, almost 2 years ago

Problem/Motivation

In #2986334: Add a way to enforce an empty result set for a database condition query we discovered that there was a query doing ->condition('1 <> 1'). This only works by accident.

For example:

$query = db_select('node');
$query->condition('1 <> 1');

Compiles to

SELECT
FROM
{node} node
WHERE 11 = :db_condition_placeholder_0"]

Proposed resolution

If only $field is passed to condition() and it is a string then throw an error because we cannot in this instance build a condition. This allows NULL to be passed in as $value but not to default to NULL in this case. That allows queries like

    $query
      ->fields('base_table', ['workspace'])
      ->condition('target_entity_type_id', $entity->getEntityTypeId())
      ->condition('target_entity_id', $entity->id());

to continue to work as there are instances where $entity->id() is null.

This should prevent literal SQL expressions like 1 <> 1 for working by accident.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs work

Version

10.1

Component
Database 

Last updated 2 days ago

  • Maintained by
  • 🇳🇱Netherlands @daffie
Created by

🇬🇧United Kingdom alexpott 🇪🇺🌍

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