Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 979
bugfix-2838-searchfilter-custom-types#2873
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) { | ||
| @@ -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)), | ||
| ]); | ||
| @@ -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; | ||
| } | ||
| @@ -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 | ||
| @@ -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); | ||
| @@ -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) | ||
| { | ||
| $wrapCase = $this->createWrapCase($caseSensitive); | ||
| $orX = $queryBuilder->expr()->orX(); | ||
| foreach ($values as $value) { | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BC: we cannot add a non-optional argument to a | ||
| { | ||
| $wrapCase = $this->createWrapCase($caseSensitive); | ||
| $valueParameter = $queryNameGenerator->generateParameterName($field); | ||
| @@ -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)); | ||
There was a problem hiding this comment.
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.