Last active
November 1, 2024 23:08
-
-
Save axelvnk/edf879af5c7dbd9616a4eeb77c7181a3 to your computer and use it in GitHub Desktop.
Api platform OR search filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Axelvkn\AppBundle\Filter; | |
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter; | |
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | |
use ApiPlatform\Core\Exception\InvalidArgumentException; | |
use Doctrine\ORM\QueryBuilder; | |
class OrSearchFilter extends SearchFilter | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $value, bool $caseSensitive) | |
{ | |
$wrapCase = $this->createWrapCase($caseSensitive); | |
$valueParameter = $queryNameGenerator->generateParameterName($field); | |
switch ($strategy) { | |
case null: | |
case self::STRATEGY_EXACT: | |
$queryBuilder | |
->orWhere(sprintf($wrapCase('%s.%s').' = '.$wrapCase(':%s'), $alias, $field, $valueParameter)) | |
->setParameter($valueParameter, $value); | |
break; | |
case self::STRATEGY_PARTIAL: | |
$queryBuilder | |
->orWhere(sprintf($wrapCase('%s.%s').' LIKE '.$wrapCase('CONCAT(\'%%\', :%s, \'%%\')'), $alias, $field, $valueParameter)) | |
->setParameter($valueParameter, $value); | |
break; | |
case self::STRATEGY_START: | |
$queryBuilder | |
->orWhere(sprintf($wrapCase('%s.%s').' LIKE '.$wrapCase('CONCAT(:%s, \'%%\')'), $alias, $field, $valueParameter)) | |
->setParameter($valueParameter, $value); | |
break; | |
case self::STRATEGY_END: | |
$queryBuilder | |
->orWhere(sprintf($wrapCase('%s.%s').' LIKE '.$wrapCase('CONCAT(\'%%\', :%s)'), $alias, $field, $valueParameter)) | |
->setParameter($valueParameter, $value); | |
break; | |
case self::STRATEGY_WORD_START: | |
$queryBuilder | |
->orWhere(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); | |
break; | |
default: | |
throw new InvalidArgumentException(sprintf('strategy %s does not exist.', $strategy)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
axelvnk.filter.or_search_filter: | |
class: Axelvnk\AppBundle\Filter\OrSearchFilter | |
parent: "api_platform.doctrine.orm.search_filter" | |
axelvnk.filter.customer: | |
parent: axelvnk.filter.or_search_filter | |
arguments: | |
- { name: "partial", vatNumber: "partial" } | |
tags: | |
- { name: "api_platform.filter", id: "customer.search" } | |
#now you can search /api/customers?name=0844.010.460&vatNumber=0844.010.460 and your filter will be applied with all or conditions! |
@UtechtDustin this code add or
conditions so it should work with other filters. Did you try it?
Maybe you don't need this filter but instead create a filter to search in several properties: https://api-platform.com/docs/core/filters/#creating-custom-filters See also this answer on Stack Overflow.
Unfortunately doesn't work with API Platform 3.0
I write this if you want :
<?php
namespace App\Filter;
use ApiPlatform\Api\IdentifiersExtractorInterface;
use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Doctrine\Common\Filter\SearchFilterInterface;
use ApiPlatform\Doctrine\Common\Filter\SearchFilterTrait;
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\Operation;
use Closure;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
final class SearchMultiFieldsFilter extends AbstractFilter implements SearchFilterInterface
{
use SearchFilterTrait;
public function __construct(
ManagerRegistry $managerRegistry,
IriConverterInterface $iriConverter,
?PropertyAccessorInterface $propertyAccessor = null,
?LoggerInterface $logger = null,
?array $properties = null,
?IdentifiersExtractorInterface $identifiersExtractor = null,
?NameConverterInterface $nameConverter = null,
public string $searchParameterName = 'search',
) {
parent::__construct($managerRegistry, $logger, $properties, $nameConverter);
$this->iriConverter = $iriConverter;
$this->identifiersExtractor = $identifiersExtractor;
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
}
protected function getIriConverter(): IriConverterInterface
{
return $this->iriConverter;
}
protected function getPropertyAccessor(): PropertyAccessorInterface
{
return $this->propertyAccessor;
}
/**
* {@inheritDoc}
*/
protected function filterProperty(
string $property,
mixed $value,
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
?Operation $operation = null,
array $context = [],
): void {
if (
null === $value
|| $property !== $this->searchParameterName
) {
return;
}
$alias = $queryBuilder->getRootAliases()[0];
$ors = [];
$count = 0;
foreach (($this->getProperties() ?? []) as $prop => $caseSensitive) {
$filter = $this->generatePropertyOrWhere(
$queryBuilder,
$queryNameGenerator,
$alias,
$prop,
$value,
$resourceClass,
$count,
$caseSensitive ?? false,
);
if (null === $filter) {
continue;
}
[$expr, $exprParams] = $filter;
$ors[] = $expr;
$queryBuilder->setParameter($exprParams[1], $exprParams[0]);
++$count;
}
$queryBuilder->andWhere($queryBuilder->expr()->orX(...$ors));
}
protected function generatePropertyOrWhere(
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $alias,
string $property,
string $value,
string $resourceClass,
int $key,
bool $caseSensitive = false,
): ?array {
if (
!$this->isPropertyEnabled($property, $resourceClass)
|| !$this->isPropertyMapped($property, $resourceClass, true)
) {
return null;
}
$field = $property;
$associations = [];
if ($this->isPropertyNested($property, $resourceClass)) {
[$alias, $field, $associations] = $this->addJoinsForNestedProperty(
$property,
$alias,
$queryBuilder,
$queryNameGenerator,
$resourceClass,
Join::INNER_JOIN,
);
}
$metadata = $this->getNestedMetadata($resourceClass, $associations);
if (
'id' === $field
|| !$metadata->hasField($field)
) {
return null;
}
$wrapCase = $this->createWrapCase($caseSensitive);
$valueParameter = ':' . $queryNameGenerator->generateParameterName($field);
$aliasedField = sprintf('%s.%s', $alias, $field);
$keyValueParameter = sprintf('%s_%s', $valueParameter, $key);
return [
$queryBuilder->expr()->like(
$wrapCase($aliasedField),
$wrapCase((string) $queryBuilder->expr()->concat("'%'", $keyValueParameter, "'%'")),
),
[$caseSensitive ? $value : strtolower($value), $keyValueParameter],
];
}
protected function createWrapCase(bool $caseSensitive): Closure
{
return static function (string $expr) use ($caseSensitive): string {
if ($caseSensitive) {
return $expr;
}
return sprintf('LOWER(%s)', $expr);
};
}
/**
* {@inheritDoc}
*/
protected function getType(string $doctrineType): string
{
return 'string';
}
public function getDescription(string $resourceClass): array
{
$props = $this->getProperties();
if (null === $props) {
throw new InvalidArgumentException('Properties must be specified');
}
return [
$this->searchParameterName => [
'property' => implode(', ', array_keys($props)),
'type' => 'string',
'required' => false,
'description' => 'Recherche sur les propriétés spécifiées.',
],
];
}
}
ApiFilter(
SearchMultiFieldsFilter::class,
properties: [
'name',
'email',
'manager.fullname',
'manager.email',
],
),
@loicngr This is absolutely on point, thanks !!
@aesislabs you're welcome
@loicngr please that is work only for string what i do if i want to search with int
Thank you very much! @loicngr
Here's another exemple using APIP 4
https://gist.github.com/nicolasbonnici/3d598c8981ec1ac9769b4b7f5a9031f6
<?php
namespace App\Filter;
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
use Doctrine\ORM\Query\Expr\Comparison;
use Doctrine\ORM\QueryBuilder;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
class SearchOrFilter implements FilterInterface
{
private ?CamelCaseToSnakeCaseNameConverter $nameConverter = null;
public function __construct(private readonly RequestStack $requestStack, private readonly array $properties = [])
{
}
private function getNameConverter(): CamelCaseToSnakeCaseNameConverter
{
return $this->nameConverter ??= new CamelCaseToSnakeCaseNameConverter();
}
public function apply(
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
$operation = null,
array $context = []
): void
{
$request = $this->requestStack->getCurrentRequest();
if (!$request) {
return;
}
$alias = $queryBuilder->getRootAliases()[0];
$orConditions = [];
foreach ($this->properties as $property => $settings) {
$type = is_array($settings) ? $settings['type'] : $settings;
$value = $request->get($this->getNameConverter()->normalize($property));
if ($value === null) {
continue;
}
$parameterName = $queryNameGenerator->generateParameterName($property);
$orConditions[] = $this->buildCondition(
$queryBuilder,
$alias,
$property,
$parameterName,
$value,
$type
);
}
if (!empty($orConditions)) {
$queryBuilder->andWhere($queryBuilder->expr()->orX(...$orConditions));
}
}
private function buildCondition(
QueryBuilder $queryBuilder,
string $alias,
string $property,
string $parameterName,
string $value,
string $type
): Comparison {
$isPartial = in_array($type, ['partial', 'ipartial'], true);
$isInsensitive = in_array($type, ['ipartial', 'iexact'], true);
$value = $isPartial ? '%' . $value . '%' : $value;
$queryBuilder->setParameter($parameterName, $value);
$column = $isInsensitive
? sprintf('LOWER(%s.%s)', $alias, $property)
: sprintf('%s.%s', $alias, $property);
$param = $isInsensitive
? sprintf('LOWER(:%s)', $parameterName)
: sprintf(':%s', $parameterName);
return $isPartial
? $queryBuilder->expr()->like($column, $param)
: $queryBuilder->expr()->eq($column, $param);
}
public function getDescription(string $resourceClass): array
{
return array_map(fn($settings, $property) => [
'property' => $property,
'type' => Type::BUILTIN_TYPE_STRING,
'required' => false,
'description' => sprintf('Filter using OR condition on %s', $property),
], $this->properties, array_keys($this->properties));
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it possible to use this Filter and the Searchfilter at the same time ?
We have on the left side a few filters e.g.
city
, but on the top we also want theOrSearchFilter
to search on almsot any property of the Entity.So if someone search for e.g.
test123
on the top search we see want to see all entities which hastest123
in at least one of the defined properties (orSearchFilter).If we also set the city filter e.g.
new york
on the left side we want to see all Entities which are located innew york
AND havetest123
in at least one of the defined properties (orSearchFilter).