Skip to content

Commit 3022501

Browse files
committed
Merge remote-tracking branch 'oleg-andreyev/custom-doctrine-type' into 2.5
2 parents e234f86 + 9204027 commit 3022501

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/Bridge/Doctrine/Orm/Filter/SearchFilter.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,11 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
9292
$caseSensitive = true;
9393
$metadata = $this->getNestedMetadata($resourceClass, $associations);
9494

95-
if ($metadata->hasField($field)) {
96-
if ('id' === $field) {
97-
$values = array_map([$this, 'getIdFromValue'], $values);
98-
}
95+
$doctrineTypeField = $this->getDoctrineFieldType($property, $resourceClass);
96+
$values = array_map([$this, 'getIdFromValue'], $values);
9997

100-
if (!$this->hasValidValues($values, $this->getDoctrineFieldType($property, $resourceClass))) {
98+
if ($metadata->hasField($field)) {
99+
if (!$this->hasValidValues($values, $doctrineTypeField)) {
101100
$this->logger->notice('Invalid filter ignored', [
102101
'exception' => new InvalidArgumentException(sprintf('Values for field "%s" are not valid according to the doctrine type.', $field)),
103102
]);
@@ -114,7 +113,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
114113
}
115114

116115
if (1 === \count($values)) {
117-
$this->addWhereByStrategy($strategy, $queryBuilder, $queryNameGenerator, $alias, $field, $values[0], $caseSensitive);
116+
$this->addWhereByStrategy($strategy, $queryBuilder, $queryNameGenerator, $alias, $field, $doctrineTypeField, $values[0], $caseSensitive);
118117

119118
return;
120119
}
@@ -140,9 +139,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
140139
return;
141140
}
142141

143-
$values = array_map([$this, 'getIdFromValue'], $values);
144142
$associationFieldIdentifier = 'id';
145-
$doctrineTypeField = $this->getDoctrineFieldType($property, $resourceClass);
146143

147144
if (null !== $this->identifiersExtractor) {
148145
$associationResourceClass = $metadata->getAssociationTargetClass($field);
@@ -171,11 +168,11 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
171168
if (1 === \count($values)) {
172169
$queryBuilder
173170
->andWhere(sprintf('%s.%s = :%s', $associationAlias, $associationField, $valueParameter))
174-
->setParameter($valueParameter, $values[0]);
171+
->setParameter($valueParameter, $values[0], $doctrineTypeField);
175172
} else {
176173
$queryBuilder
177174
->andWhere(sprintf('%s.%s IN (:%s)', $associationAlias, $associationField, $valueParameter))
178-
->setParameter($valueParameter, $values);
175+
->setParameter($valueParameter, $values, $doctrineTypeField);
179176
}
180177
}
181178

@@ -184,7 +181,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
184181
*
185182
* @throws InvalidArgumentException If strategy does not exist
186183
*/
187-
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $value, bool $caseSensitive)
184+
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $fieldType, $value, bool $caseSensitive)
188185
{
189186
$wrapCase = $this->createWrapCase($caseSensitive);
190187
$valueParameter = $queryNameGenerator->generateParameterName($field);
@@ -194,27 +191,27 @@ protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuild
194191
case self::STRATEGY_EXACT:
195192
$queryBuilder
196193
->andWhere(sprintf($wrapCase('%s.%s').' = '.$wrapCase(':%s'), $alias, $field, $valueParameter))
197-
->setParameter($valueParameter, $value);
194+
->setParameter($valueParameter, $value, $fieldType);
198195
break;
199196
case self::STRATEGY_PARTIAL:
200197
$queryBuilder
201198
->andWhere(sprintf($wrapCase('%s.%s').' LIKE '.$wrapCase('CONCAT(\'%%\', :%s, \'%%\')'), $alias, $field, $valueParameter))
202-
->setParameter($valueParameter, $value);
199+
->setParameter($valueParameter, $value, $fieldType);
203200
break;
204201
case self::STRATEGY_START:
205202
$queryBuilder
206203
->andWhere(sprintf($wrapCase('%s.%s').' LIKE '.$wrapCase('CONCAT(:%s, \'%%\')'), $alias, $field, $valueParameter))
207-
->setParameter($valueParameter, $value);
204+
->setParameter($valueParameter, $value, $fieldType);
208205
break;
209206
case self::STRATEGY_END:
210207
$queryBuilder
211208
->andWhere(sprintf($wrapCase('%s.%s').' LIKE '.$wrapCase('CONCAT(\'%%\', :%s)'), $alias, $field, $valueParameter))
212-
->setParameter($valueParameter, $value);
209+
->setParameter($valueParameter, $value, $fieldType);
213210
break;
214211
case self::STRATEGY_WORD_START:
215212
$queryBuilder
216213
->andWhere(sprintf($wrapCase('%1$s.%2$s').' LIKE '.$wrapCase('CONCAT(:%3$s, \'%%\')').' OR '.$wrapCase('%1$s.%2$s').' LIKE '.$wrapCase('CONCAT(\'%% \', :%3$s, \'%%\')'), $alias, $field, $valueParameter))
217-
->setParameter($valueParameter, $value);
214+
->setParameter($valueParameter, $value, $fieldType);
218215
break;
219216
default:
220217
throw new InvalidArgumentException(sprintf('strategy %s does not exist.', $strategy));

0 commit comments

Comments
 (0)