function db_like in Drupal 8
Same name and namespace in other branches
- 7 includes/database/database.inc \db_like()
Escapes characters that work as wildcard characters in a LIKE pattern.
The wildcard characters "%" and "_" as well as backslash are prefixed with a backslash. Use this to do a search for a verbatim string without any wildcard behavior.
You must use a query builder like \Drupal::database()->select() in order to use \Drupal::database()->escapeLike() on all supported database systems. Using \Drupal::database()->escapeLike() with \Drupal::database()->query() or \Drupal::database()->queryRange() is not supported.
For example, the following does a case-insensitive query for all rows whose name starts with $prefix:
$result = \Drupal::database()
->select('person', 'p')
->fields('p')
->condition('name', db_like($prefix) . '%', 'LIKE')
->execute()
->fetchAll();
Backslash is defined as escape character for LIKE patterns in DatabaseCondition::mapConditionOperator().
Parameters
string $string: The string to escape.
Return value
string The escaped string.
Deprecated
in drupal:8.0.0 and is removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call escapeLike() on it. For example, $injected_database->escapeLike($string);
See also
https://www.drupal.org/node/2993033
\Drupal\Core\Database\Connection::escapeLike()
Related topics
1 call to db_like()
- DatabaseLegacyTest::testDbLike in core/
tests/ Drupal/ KernelTests/ Core/ Database/ DatabaseLegacyTest.php - Tests deprecation of the db_like() function.
File
- core/
includes/ database.inc, line 418 - Core systems for the database layer.
Code
function db_like($string) {
@trigger_error('db_like() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call escapeLike() on it. For example, $injected_database->escapeLike($string). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()
->escapeLike($string);
}