Reference
C
Class

ApiPlatform\Doctrine\Orm\Filter\SearchFilter

The search filter allows to filter a collection by given properties.

The search filter supports exact, partial, start, end, and word_start matching strategies:

  • exact strategy searches for fields that exactly match the value
  • partial strategy uses LIKE %value% to search for fields that contain the value
  • start strategy uses LIKE value% to search for fields that start with the value
  • end strategy uses LIKE %value to search for fields that end with the value
  • word_start strategy uses LIKE value% OR LIKE % value% to search for fields that contain words starting with the value

Note: it is possible to filter on properties and relations too.

Prepend the letter i to the filter if you want it to be case-insensitive. For example ipartial or iexact. Note that this will use the LOWER function and will impact performance if there is no proper index.

Case insensitivity may already be enforced at the database level depending on the collation used. If you are using MySQL, note that the commonly used utf8_unicode_ci collation (and its sibling utf8mb4_unicode_ci) are already case-insensitive, as indicated by the _ci part in their names.

Note: Search filters with the exact strategy can have multiple values for the same property (in this case the condition will be similar to a SQL IN clause).

Syntax: ?property[]=foo&property[]=bar.

<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
 
#[ApiResource]
#[ApiFilter(SearchFilter::class, properties: ['isbn' => 'exact', 'description' => 'partial'])]
class Book
{
    // ...
}
<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
 
#[ApiResource]
#[ApiFilter(SearchFilter::class, properties: ['isbn' => 'exact', 'description' => 'partial'])]
class Book
{
    // ...
}
class ApiPlatform\Doctrine\Orm\Filter\SearchFilter extends ApiPlatform\Doctrine\Orm\Filter\AbstractFilter implements `<a href="/docs/reference/Doctrine/Orm/Filter/FilterInterface">ApiPlatform\Doctrine\Orm\Filter\FilterInterface</a>`, `<a href="/docs/reference/Metadata/FilterInterface">ApiPlatform\Metadata\FilterInterface</a>`, `<a href="/docs/reference/Doctrine/Common/Filter/SearchFilterInterface">ApiPlatform\Doctrine\Common\Filter\SearchFilterInterface</a>`
{
    public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, ApiPlatform\Api\IriConverterInterface $iriConverter, null|Symfony\Component\PropertyAccess\PropertyAccessorInterface $propertyAccessor, null|Psr\Log\LoggerInterface $logger, null|array $properties, null|ApiPlatform\Api\IdentifiersExtractorInterface $identifiersExtractor, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter)
    protected getIriConverter(): ApiPlatform\Api\IriConverterInterface
    protected getPropertyAccessor(): Symfony\Component\PropertyAccess\PropertyAccessorInterface
    protected filterProperty(string $property, $value, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
    protected addWhereByStrategy(string $strategy, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $values, bool $caseSensitive): null
    protected createWrapCase(bool $caseSensitive): Closure
    protected getType(string $doctrineType): string
    public apply(Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
    protected getManagerRegistry(): Doctrine\Persistence\ManagerRegistry
    protected getProperties(): array
    protected getLogger(): Psr\Log\LoggerInterface
    protected isPropertyEnabled(string $property, string $resourceClass): bool
    protected denormalizePropertyName(string|int $property): string
    protected normalizePropertyName(string $property): string
    protected splitPropertyParts(string $property, string $resourceClass): array
    protected addJoinsForNestedProperty(string $property, string $rootAlias, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $joinType): array
    protected isPropertyMapped(string $property, string $resourceClass, bool $allowAssociation): bool
    protected isPropertyNested(string $property, string $resourceClass): bool
    protected isPropertyEmbedded(string $property, string $resourceClass): bool
    protected getDoctrineFieldType(string $property, string $resourceClass): string
    protected getNestedMetadata(string $resourceClass, array&lt;int, string&gt; $associations): Doctrine\Persistence\Mapping\ClassMetadata
    protected getClassMetadata(string $resourceClass): Doctrine\Persistence\Mapping\ClassMetadata
    public getDescription(string $resourceClass): array
    protected getIdFromValue(string $value)
    protected normalizeValues(array $values, string $property): array
    protected hasValidValues(array $values, null|string $type): bool
}
class ApiPlatform\Doctrine\Orm\Filter\SearchFilter extends ApiPlatform\Doctrine\Orm\Filter\AbstractFilter implements `<a href="/docs/reference/Doctrine/Orm/Filter/FilterInterface">ApiPlatform\Doctrine\Orm\Filter\FilterInterface</a>`, `<a href="/docs/reference/Metadata/FilterInterface">ApiPlatform\Metadata\FilterInterface</a>`, `<a href="/docs/reference/Doctrine/Common/Filter/SearchFilterInterface">ApiPlatform\Doctrine\Common\Filter\SearchFilterInterface</a>`
{
    public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, ApiPlatform\Api\IriConverterInterface $iriConverter, null|Symfony\Component\PropertyAccess\PropertyAccessorInterface $propertyAccessor, null|Psr\Log\LoggerInterface $logger, null|array $properties, null|ApiPlatform\Api\IdentifiersExtractorInterface $identifiersExtractor, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter)
    protected getIriConverter(): ApiPlatform\Api\IriConverterInterface
    protected getPropertyAccessor(): Symfony\Component\PropertyAccess\PropertyAccessorInterface
    protected filterProperty(string $property, $value, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
    protected addWhereByStrategy(string $strategy, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $values, bool $caseSensitive): null
    protected createWrapCase(bool $caseSensitive): Closure
    protected getType(string $doctrineType): string
    public apply(Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
    protected getManagerRegistry(): Doctrine\Persistence\ManagerRegistry
    protected getProperties(): array
    protected getLogger(): Psr\Log\LoggerInterface
    protected isPropertyEnabled(string $property, string $resourceClass): bool
    protected denormalizePropertyName(string|int $property): string
    protected normalizePropertyName(string $property): string
    protected splitPropertyParts(string $property, string $resourceClass): array
    protected addJoinsForNestedProperty(string $property, string $rootAlias, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $joinType): array
    protected isPropertyMapped(string $property, string $resourceClass, bool $allowAssociation): bool
    protected isPropertyNested(string $property, string $resourceClass): bool
    protected isPropertyEmbedded(string $property, string $resourceClass): bool
    protected getDoctrineFieldType(string $property, string $resourceClass): string
    protected getNestedMetadata(string $resourceClass, array&lt;int, string&gt; $associations): Doctrine\Persistence\Mapping\ClassMetadata
    protected getClassMetadata(string $resourceClass): Doctrine\Persistence\Mapping\ClassMetadata
    public getDescription(string $resourceClass): array
    protected getIdFromValue(string $value)
    protected normalizeValues(array $values, string $property): array
    protected hasValidValues(array $values, null|string $type): bool
}

Properties

logger

Psr\Log\LoggerInterface $logger
Psr\Log\LoggerInterface $logger

managerRegistry

Doctrine\Persistence\ManagerRegistry $managerRegistry
Doctrine\Persistence\ManagerRegistry $managerRegistry

properties

array $properties
array $properties

nameConverter

Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter
Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter

iriConverter

ApiPlatform\Api\IriConverterInterface $iriConverter
ApiPlatform\Api\IriConverterInterface $iriConverter

propertyAccessor

Symfony\Component\PropertyAccess\PropertyAccessorInterface $propertyAccessor
Symfony\Component\PropertyAccess\PropertyAccessorInterface $propertyAccessor

identifiersExtractor

ApiPlatform\Api\IdentifiersExtractorInterface $identifiersExtractor
ApiPlatform\Api\IdentifiersExtractorInterface $identifiersExtractor

Methods

__construct

public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, ApiPlatform\Api\IriConverterInterface $iriConverter, null|Symfony\Component\PropertyAccess\PropertyAccessorInterface $propertyAccessor, null|Psr\Log\LoggerInterface $logger, null|array $properties, null|ApiPlatform\Api\IdentifiersExtractorInterface $identifiersExtractor, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter)
public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, ApiPlatform\Api\IriConverterInterface $iriConverter, null|Symfony\Component\PropertyAccess\PropertyAccessorInterface $propertyAccessor, null|Psr\Log\LoggerInterface $logger, null|array $properties, null|ApiPlatform\Api\IdentifiersExtractorInterface $identifiersExtractor, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter)

Parameters

managerRegistryDoctrine\Persistence\ManagerRegistry
iriConverterApiPlatform\Api\IriConverterInterface
propertyAccessorSymfony\Component\PropertyAccess\PropertyAccessorInterface
loggerPsr\Log\LoggerInterface
propertiesarray
identifiersExtractorApiPlatform\Api\IdentifiersExtractorInterface
nameConverterSymfony\Component\Serializer\NameConverter\NameConverterInterface

getIriConverter

protected getIriConverter(): ApiPlatform\Api\IriConverterInterface
protected getIriConverter(): ApiPlatform\Api\IriConverterInterface

Returns

ApiPlatform\Api\IriConverterInterface

getPropertyAccessor

protected getPropertyAccessor(): Symfony\Component\PropertyAccess\PropertyAccessorInterface
protected getPropertyAccessor(): Symfony\Component\PropertyAccess\PropertyAccessorInterface

Returns

Symfony\Component\PropertyAccess\PropertyAccessorInterface

filterProperty

Passes a property through the filter.

protected filterProperty(string $property, $value, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
protected filterProperty(string $property, $value, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null

Parameters

propertystring
value
queryBuilderDoctrine\ORM\QueryBuilder
queryNameGeneratorApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface
resourceClassstring
operationApiPlatform\Metadata\Operation
contextarray

Returns

null

addWhereByStrategy

Adds where clause according to the strategy.

protected addWhereByStrategy(string $strategy, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $values, bool $caseSensitive): null
protected addWhereByStrategy(string $strategy, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $values, bool $caseSensitive): null

Parameters

strategystring
queryBuilderDoctrine\ORM\QueryBuilder
queryNameGeneratorApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface
aliasstring
fieldstring
values
caseSensitivebool

Returns

null

createWrapCase

Creates a function that will wrap a Doctrine expression according to the specified case sensitivity.For example, "o.name" will get wrapped into "LOWER(o.name)" when $caseSensitive is false.

protected createWrapCase(bool $caseSensitive): Closure
protected createWrapCase(bool $caseSensitive): Closure

Parameters

caseSensitivebool

Returns

Closure

getType

protected getType(string $doctrineType): string
protected getType(string $doctrineType): string

Parameters

doctrineTypestring

Returns

string

splitPropertyParts

Splits the given property into parts.Returns an array with the following keys:

  • associations: array of associations according to nesting order
  • field: string holding the actual field (leaf node)
protected splitPropertyParts(string $property, string $resourceClass): array
protected splitPropertyParts(string $property, string $resourceClass): array

Parameters

propertystring
resourceClassstring

Returns

array

isPropertyMapped

Determines whether the given property is mapped.

protected isPropertyMapped(string $property, string $resourceClass, bool $allowAssociation): bool
protected isPropertyMapped(string $property, string $resourceClass, bool $allowAssociation): bool

Parameters

propertystring
resourceClassstring
allowAssociationbool

Returns

bool

isPropertyNested

Determines whether the given property is nested.

protected isPropertyNested(string $property, string $resourceClass): bool
protected isPropertyNested(string $property, string $resourceClass): bool

Parameters

propertystring
resourceClassstring

Returns

bool

isPropertyEmbedded

Determines whether the given property is embedded.

protected isPropertyEmbedded(string $property, string $resourceClass): bool
protected isPropertyEmbedded(string $property, string $resourceClass): bool

Parameters

propertystring
resourceClassstring

Returns

bool

getDoctrineFieldType

Gets the Doctrine Type of a given property/resourceClass.

protected getDoctrineFieldType(string $property, string $resourceClass): string
protected getDoctrineFieldType(string $property, string $resourceClass): string

Parameters

propertystring
resourceClassstring

Returns

string

getNestedMetadata

Gets nested class metadata for the given resource.

protected getNestedMetadata(string $resourceClass, array&lt;int, string&gt; $associations): Doctrine\Persistence\Mapping\ClassMetadata
protected getNestedMetadata(string $resourceClass, array&lt;int, string&gt; $associations): Doctrine\Persistence\Mapping\ClassMetadata

Parameters

resourceClassstring
associationsarray<int, string>

Returns

Doctrine\Persistence\Mapping\ClassMetadata

getClassMetadata

Gets class metadata for the given resource.

protected getClassMetadata(string $resourceClass): Doctrine\Persistence\Mapping\ClassMetadata
protected getClassMetadata(string $resourceClass): Doctrine\Persistence\Mapping\ClassMetadata

Parameters

resourceClassstring

Returns

Doctrine\Persistence\Mapping\ClassMetadata

getDescription

Gets the description of this filter for the given resource.Returns an array with the filter parameter names as keys and array with the following data as values:

  • property: the property where the filter is applied
  • type: the type of the filter
  • required: if this filter is required
  • strategy (optional): the used strategy
  • is_collection (optional): if this filter is for collection
  • swagger (optional): additional parameters for the path operation, e.g. 'swagger' => [ 'description' => 'My Description', 'name' => 'My Name', 'type' => 'integer', ]
  • openapi (optional): additional parameters for the path operation in the version 3 spec, e.g. 'openapi' => [ 'description' => 'My Description', 'name' => 'My Name', 'schema' => [ 'type' => 'integer', ] ]
  • schema (optional): schema definition, e.g. 'schema' => [ 'type' => 'string', 'enum' => ['value_1', 'value_2'], ] The description can contain additional data specific to a filter.Gets the description of this filter for the given resource.Returns an array with the filter parameter names as keys and array with the following data as values:
  • property: the property where the filter is applied
  • type: the type of the filter
  • required: if this filter is required
  • strategy (optional): the used strategy
  • is_collection (optional): if this filter is for collection
  • swagger (optional): additional parameters for the path operation, e.g. 'swagger' => [ 'description' => 'My Description', 'name' => 'My Name', 'type' => 'integer', ]
  • openapi (optional): additional parameters for the path operation in the version 3 spec, e.g. 'openapi' => [ 'description' => 'My Description', 'name' => 'My Name', 'schema' => [ 'type' => 'integer', ] ]
  • schema (optional): schema definition, e.g. 'schema' => [ 'type' => 'string', 'enum' => ['value_1', 'value_2'], ] The description can contain additional data specific to a filter.Gets the description of this filter for the given resource.Returns an array with the filter parameter names as keys and array with the following data as values:
  • property: the property where the filter is applied
  • type: the type of the filter
  • required: if this filter is required
  • strategy (optional): the used strategy
  • is_collection (optional): if this filter is for collection
  • swagger (optional): additional parameters for the path operation, e.g. 'swagger' => [ 'description' => 'My Description', 'name' => 'My Name', 'type' => 'integer', ]
  • openapi (optional): additional parameters for the path operation in the version 3 spec, e.g. 'openapi' => [ 'description' => 'My Description', 'name' => 'My Name', 'schema' => [ 'type' => 'integer', ] ]
  • schema (optional): schema definition, e.g. 'schema' => [ 'type' => 'string', 'enum' => ['value_1', 'value_2'], ] The description can contain additional data specific to a filter.
public getDescription(string $resourceClass): array
public getDescription(string $resourceClass): array

Parameters

resourceClassstring

Returns

array

getIdFromValue

Gets the ID from an IRI or a raw ID.

protected getIdFromValue(string $value)
protected getIdFromValue(string $value)

Parameters

valuestring

Returns

normalizeValues

Normalize the values array.

protected normalizeValues(array $values, string $property): array
protected normalizeValues(array $values, string $property): array

Parameters

valuesarray
propertystring

Returns

array

hasValidValues

When the field should be an integer, check that the given value is a valid one.

protected hasValidValues(array $values, null|string $type): bool
protected hasValidValues(array $values, null|string $type): bool

Parameters

valuesarray
typestring

Returns

bool

Copyright © 2023 Kévin Dunglas

Sponsored by Les-Tilleuls.coop