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')
and the data from hook_schema. 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
: 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