Skip to content

Commit

Permalink
Escape underscores in generated like conditions
Browse files Browse the repository at this point in the history
Resolves #11898
  • Loading branch information
brandonkelly committed Sep 8, 2022
1 parent f3e77c5 commit 214255e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Changed
- `resave/*` commands now have a `--touch` option. When passed, elements’ `dateUpdated` timestamps will be updated as they’re resaved. ([#11849](https://github.com/craftcms/cms/discussions/11849))
- Underscores within query param values that begin/end with `*` are now escaped, so they aren’t treated as wildcard characters by the `like` condition. ([#11898](https://github.com/craftcms/cms/issues/11898))
- `craft\services\Elements::resaveElements()` now has a `$touch` argument.

### Fixed
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@ public static function parseParam(string $column, $value, string $defaultOperato
} else {
$operator = $operator === '=' ? 'like' : 'not like';
}

// Escape underscores as they are treated as wildcards by LIKE in MySQL and PostgreSQL
$val = preg_replace('/(?<!\\\)_/', '\\_', $val);

$condition[] = [$operator, $column, $val, false];
continue;
}
Expand Down

0 comments on commit 214255e

Please sign in to comment.