Skip to content

Commit

Permalink
fix(doctrine): use abitrary index instead of value (#5079)
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil authored Oct 24, 2022
1 parent 541b738 commit 7044c5a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Doctrine/Orm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuild
$parameters = [];
foreach ($values as $key => $value) {
$keyValueParameter = sprintf('%s_%s', $valueParameter, $key);
$parameters[$caseSensitive ? $value : strtolower($value)] = $keyValueParameter;
$parameters[] = [$caseSensitive ? $value : strtolower($value), $keyValueParameter];

$ors[] = match ($strategy) {
self::STRATEGY_PARTIAL => $queryBuilder->expr()->like(
Expand Down Expand Up @@ -209,7 +209,9 @@ protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuild
}

$queryBuilder->andWhere($queryBuilder->expr()->orX(...$ors));
array_walk($parameters, $queryBuilder->setParameter(...));
foreach ($parameters as $parameter) {
$queryBuilder->setParameter($parameter[1], $parameter[0]);
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Doctrine/Common/Filter/SearchFilterTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ private function provideApplyTestArguments(): array
],
],
],
'partial (multiple almost same values; case insensitive)' => [
[
'id' => null,
'name' => 'ipartial',
],
[
'name' => [
'blue car',
'Blue Car',
],
],
],
'start' => [
[
'id' => null,
Expand Down
15 changes: 15 additions & 0 deletions tests/Doctrine/Odm/Filter/SearchFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,21 @@ public function provideApplyTestData(): array
],
$filterFactory,
],
'partial (multiple almost same values; case insensitive)' => [
[
[
'$match' => [
'name' => [
'$in' => [
new Regex('blue car', 'i'),
new Regex('Blue Car', 'i'),
],
],
],
],
],
$filterFactory,
],
'start' => [
[
[
Expand Down
8 changes: 8 additions & 0 deletions tests/Doctrine/Orm/Filter/SearchFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,14 @@ public function provideApplyTestData(): array
],
$filterFactory,
],
'partial (multiple almost same values; case insensitive)' => [
sprintf('SELECT %s FROM %s %1$s WHERE LOWER(%1$s.name) LIKE LOWER(CONCAT(\'%%\', :name_p1_0, \'%%\')) OR LOWER(%1$s.name) LIKE LOWER(CONCAT(\'%%\', :name_p1_1, \'%%\'))', $this->alias, Dummy::class),
[
'name_p1_0' => 'blue car',
'name_p1_1' => 'blue car',
],
$filterFactory,
],
'start' => [
sprintf('SELECT %s FROM %s %1$s WHERE %1$s.name LIKE CONCAT(:name_p1_0, \'%%\')', $this->alias, Dummy::class),
['name_p1_0' => 'partial'],
Expand Down

0 comments on commit 7044c5a

Please sign in to comment.