Problem/Motivation

MongoDB needs query condition parameters to be of the correct type. For instance when you create a new node entity and it has the ID = 4. You can with a relational database query the node table by doing:

$nid = '4';
$result = \Drupal:database()->select('node', 'n')
  ->fields('n', ['nid'])
  ->condition('nid', $nid)
  ->execute()
  ->fetchField();

With MongoDB the value of $result will be NULL. For MongoDB the integer 4 is not the same as the string value '4'.

Proposed resolution

Give parameter values in query conditions the correct typehint. With the correct typehint the query will work for relational databases and for MongoDB.

$nid = '4';
$result = \Drupal:database()->select('node', 'n')
  ->fields('n', ['nid'])
  ->condition('nid', (int) $nid)
  ->execute()
  ->fetchField();

To test if this is done correctly we can throw a deprecation message and replace it with an exception in the next major version when the query condition parameter is of the wrong type. Whe should do thids only in testing. The query condition parameter type checking will take up to much time in production. We can add a new variable to the class Drupal\Core\Database\Connection and name it inTesting with a default value of FALSE. When we are in testing we change the value to TRUE. A new Select query is started from the Connection class and we can give the class Select also an extra variable called inTesting which is set on construction. When not set it will default to FALSE. When we execute a query we than also do the check query condition parameters of being of the right type. We have the value of the class variable Drupal\Core\Database\Query\Select::tables and we have entity table schema in \Drupal::keyValue('entity.storage_schema.sql'). If we combine them we should have enough info to check for the correct query condition parameter types.

Remaining tasks

TBD

User interface changes

None

API changes

Only in testing: Throw a deprecation message and in a new major version an exception when a query condition parameter is of the wrong type.

Data model changes

None

Release notes snippet

TBD

Issue fork drupal-3335556

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

daffie created an issue. See original summary.

daffie’s picture

Title: Check query parameter type for the interger type and for entity type tables only » Check query parameter type for the interger type in select queries and for entity type tables only
prabuela’s picture

Assigned: Unassigned » prabuela
prabuela’s picture

Assigned: prabuela » Unassigned

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

govind_giri_goswami made their first commit to this issue’s fork.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.