Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix-2838-searchfilter-custom-types #2873

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/Bridge/Doctrine/Orm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
[$alias, $field, $associations] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass);
}
$metadata = $this->getNestedMetadata($resourceClass, $associations);
$doctrineType = $this->getDoctrineFieldType($property, $resourceClass);

$values = $this->normalizeValues((array) $value, $property);
if (null === $values) {
Expand All @@ -90,7 +91,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
$values = array_map([$this, 'getIdFromValue'], $values);
}

if (!$this->hasValidValues($values, $this->getDoctrineFieldType($property, $resourceClass))) {
if (!$this->hasValidValues($values, $doctrineType)) {
$this->logger->notice('Invalid filter ignored', [
'exception' => new InvalidArgumentException(sprintf('Values for field "%s" are not valid according to the doctrine type.', $field)),
]);
Expand All @@ -107,7 +108,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
}

if (1 === \count($values)) {
$this->addWhereByStrategy($strategy, $queryBuilder, $queryNameGenerator, $alias, $field, $values[0], $caseSensitive);
$this->addWhereByStrategy($strategy, $queryBuilder, $queryNameGenerator, $alias, $field, $values[0], $caseSensitive, $doctrineType);

return;
}
Expand All @@ -120,12 +121,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
return;
}

$wrapCase = $this->createWrapCase($caseSensitive);
$valueParameter = $queryNameGenerator->generateParameterName($field);

$queryBuilder
->andWhere(sprintf($wrapCase('%s.%s').' IN (:%s)', $alias, $field, $valueParameter))
->setParameter($valueParameter, $caseSensitive ? $values : array_map('strtolower', $values));
$this->setOrClause($queryBuilder, $queryNameGenerator, $values, $alias, $field, $doctrineType, $caseSensitive);
}

// metadata doesn't have the field, nor an association on the field
Expand All @@ -144,7 +140,6 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
}

$association = $field;
$valueParameter = $queryNameGenerator->generateParameterName($association);

if ($metadata->isCollectionValuedAssociation($association)) {
$associationAlias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $alias, $association);
Expand All @@ -155,22 +150,33 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
}

if (1 === \count($values)) {
$valueParameter = $queryNameGenerator->generateParameterName($association);
$queryBuilder
->andWhere(sprintf('%s.%s = :%s', $associationAlias, $associationField, $valueParameter))
->setParameter($valueParameter, $values[0]);
->setParameter($valueParameter, $values[0], $doctrineType);
} else {
$queryBuilder
->andWhere(sprintf('%s.%s IN (:%s)', $associationAlias, $associationField, $valueParameter))
->setParameter($valueParameter, $values);
$this->setOrClause($queryBuilder, $queryNameGenerator, $values, $associationAlias, $associationField, $doctrineType, $caseSensitive);
}
}

private function setOrClause(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, array $values, $alias, $field, $doctrineType, $caseSensitive)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This method should be removed.

{
$wrapCase = $this->createWrapCase($caseSensitive);
$orX = $queryBuilder->expr()->orX();
foreach ($values as $value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid dynamic queries like this.

$valueParameter = $queryNameGenerator->generateParameterName($field);
$orX->add(sprintf($wrapCase('%s.%s').' = :%s', $alias, $field, $valueParameter));
$queryBuilder->setParameter($valueParameter, $caseSensitive ? $value : strtolower($value), $doctrineType);
}
$queryBuilder->andWhere($orX);
}

/**
* Adds where clause according to the strategy.
*
* @throws InvalidArgumentException If strategy does not exist
*/
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $value, bool $caseSensitive)
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $value, bool $caseSensitive, $doctrineType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BC: we cannot add a non-optional argument to a protected class method.

{
$wrapCase = $this->createWrapCase($caseSensitive);
$valueParameter = $queryNameGenerator->generateParameterName($field);
Expand All @@ -180,27 +186,27 @@ protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuild
case self::STRATEGY_EXACT:
$queryBuilder
->andWhere(sprintf($wrapCase('%s.%s').' = '.$wrapCase(':%s'), $alias, $field, $valueParameter))
->setParameter($valueParameter, $value);
->setParameter($valueParameter, $value, $doctrineType);
break;
case self::STRATEGY_PARTIAL:
$queryBuilder
->andWhere(sprintf($wrapCase('%s.%s').' LIKE '.$wrapCase('CONCAT(\'%%\', :%s, \'%%\')'), $alias, $field, $valueParameter))
->setParameter($valueParameter, $value);
->setParameter($valueParameter, $value, $doctrineType);
break;
case self::STRATEGY_START:
$queryBuilder
->andWhere(sprintf($wrapCase('%s.%s').' LIKE '.$wrapCase('CONCAT(:%s, \'%%\')'), $alias, $field, $valueParameter))
->setParameter($valueParameter, $value);
->setParameter($valueParameter, $value, $doctrineType);
break;
case self::STRATEGY_END:
$queryBuilder
->andWhere(sprintf($wrapCase('%s.%s').' LIKE '.$wrapCase('CONCAT(\'%%\', :%s)'), $alias, $field, $valueParameter))
->setParameter($valueParameter, $value);
->setParameter($valueParameter, $value, $doctrineType);
break;
case self::STRATEGY_WORD_START:
$queryBuilder
->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))
->setParameter($valueParameter, $value);
->setParameter($valueParameter, $value, $doctrineType);
break;
default:
throw new InvalidArgumentException(sprintf('strategy %s does not exist.', $strategy));
Expand Down