Guides
Tutorials
The order filter allows to sort a collection against the given properties.
Syntax: ?order[property]=<asc|desc>
.
<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
#[ApiResource]
#[ApiFilter(OrderFilter::class, properties: ['id', 'title'], arguments: ['orderParameterName' => 'order'])]
class Book
{
// ...
}
<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
#[ApiResource]
#[ApiFilter(OrderFilter::class, properties: ['id', 'title'], arguments: ['orderParameterName' => 'order'])]
class Book
{
// ...
}
# config/services.yaml
services:
book.order_filter:
parent: 'api_platform.doctrine.orm.order_filter'
arguments: [ $properties: { id: ~, title: ~ }, $orderParameterName: order ]
tags: [ 'api_platform.filter' ]
# The following are mandatory only if a _defaults section is defined with inverted values.
# You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
autowire: false
autoconfigure: false
public: false
# api/config/api_platform/resources.yaml
resources:
App\Entity\Book:
- operations:
ApiPlatform\Metadata\GetCollection:
filters: ['book.order_filter']
# config/services.yaml
services:
book.order_filter:
parent: 'api_platform.doctrine.orm.order_filter'
arguments: [ $properties: { id: ~, title: ~ }, $orderParameterName: order ]
tags: [ 'api_platform.filter' ]
# The following are mandatory only if a _defaults section is defined with inverted values.
# You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
autowire: false
autoconfigure: false
public: false
# api/config/api_platform/resources.yaml
resources:
App\Entity\Book:
- operations:
ApiPlatform\Metadata\GetCollection:
filters: ['book.order_filter']
<?xml version="1.0" encoding="UTF-8" ?>
<!-- api/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container
xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="book.order_filter" parent="api_platform.doctrine.orm.order_filter">
<argument type="collection" key="properties">
<argument key="id"/>
<argument key="title"/>
</argument>
<argument key="orderParameterName">order</argument>
<tag name="api_platform.filter"/>
</service>
</services>
</container>
<!-- api/config/api_platform/resources.xml -->
<resources
xmlns="https://api-platform.com/schema/metadata/resources-3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
https://api-platform.com/schema/metadata/resources-3.0.xsd">
<resource class="App\Entity\Book">
<operations>
<operation class="ApiPlatform\Metadata\GetCollection">
<filters>
<filter>book.order_filter</filter>
</filters>
</operation>
</operations>
</resource>
</resources>
<?xml version="1.0" encoding="UTF-8" ?>
<!-- api/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container
xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="book.order_filter" parent="api_platform.doctrine.orm.order_filter">
<argument type="collection" key="properties">
<argument key="id"/>
<argument key="title"/>
</argument>
<argument key="orderParameterName">order</argument>
<tag name="api_platform.filter"/>
</service>
</services>
</container>
<!-- api/config/api_platform/resources.xml -->
<resources
xmlns="https://api-platform.com/schema/metadata/resources-3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
https://api-platform.com/schema/metadata/resources-3.0.xsd">
<resource class="App\Entity\Book">
<operations>
<operation class="ApiPlatform\Metadata\GetCollection">
<filters>
<filter>book.order_filter</filter>
</filters>
</operation>
</operations>
</resource>
</resources>
Given that the collection endpoint is /books
, you can filter books by title in ascending order and then by ID in descending order with the following query: /books?order[title]=desc&order[id]=asc
.
By default, whenever the query does not specify the direction explicitly (e.g.: /books?order[title]&order[id]
), filters will not be applied unless you configure a default order direction to use:
[codeSelector]
<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
#[ApiResource]
#[ApiFilter(OrderFilter::class, properties: ['id' => 'ASC', 'title' => 'DESC'])]
class Book
{
// ...
}
<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
#[ApiResource]
#[ApiFilter(OrderFilter::class, properties: ['id' => 'ASC', 'title' => 'DESC'])]
class Book
{
// ...
}
# config/services.yaml
services:
book.order_filter:
parent: 'api_platform.doctrine.orm.order_filter'
arguments: [ { id: ASC, title: DESC } ]
tags: [ 'api_platform.filter' ]
# The following are mandatory only if a _defaults section is defined with inverted values.
# You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
autowire: false
autoconfigure: false
public: false
# api/config/api_platform/resources.yaml
resources:
App\Entity\Book:
- operations:
ApiPlatform\Metadata\GetCollection:
filters: ['book.order_filter']
# config/services.yaml
services:
book.order_filter:
parent: 'api_platform.doctrine.orm.order_filter'
arguments: [ { id: ASC, title: DESC } ]
tags: [ 'api_platform.filter' ]
# The following are mandatory only if a _defaults section is defined with inverted values.
# You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
autowire: false
autoconfigure: false
public: false
# api/config/api_platform/resources.yaml
resources:
App\Entity\Book:
- operations:
ApiPlatform\Metadata\GetCollection:
filters: ['book.order_filter']
<?xml version="1.0" encoding="UTF-8" ?>
<!-- api/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container
xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="book.order_filter" parent="api_platform.doctrine.orm.order_filter">
<argument type="collection">
<argument key="id">ASC</argument>
<argument key="title">DESC</argument>
</argument>
<tag name="api_platform.filter"/>
</service>
</services>
</container>
<!-- api/config/api_platform/resources.xml -->
<resources
xmlns="https://api-platform.com/schema/metadata/resources-3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
https://api-platform.com/schema/metadata/resources-3.0.xsd">
<resource class="App\Entity\Book">
<operations>
<operation class="ApiPlatform\Metadata\GetCollection">
<filters>
<filter>book.order_filter</filter>
</filters>
</operation>
</operations>
</resource>
</resources>
<?xml version="1.0" encoding="UTF-8" ?>
<!-- api/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container
xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="book.order_filter" parent="api_platform.doctrine.orm.order_filter">
<argument type="collection">
<argument key="id">ASC</argument>
<argument key="title">DESC</argument>
</argument>
<tag name="api_platform.filter"/>
</service>
</services>
</container>
<!-- api/config/api_platform/resources.xml -->
<resources
xmlns="https://api-platform.com/schema/metadata/resources-3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
https://api-platform.com/schema/metadata/resources-3.0.xsd">
<resource class="App\Entity\Book">
<operations>
<operation class="ApiPlatform\Metadata\GetCollection">
<filters>
<filter>book.order_filter</filter>
</filters>
</operation>
</operations>
</resource>
</resources>
[/codeSelector]
When the property used for ordering can contain null
values, you may want to specify how null
values are treated in the comparison:
null
strategyApiPlatform\Doctrine\Orm\Filter\OrderFilter::NULLS_SMALLEST
(nulls_smallest
) strategyApiPlatform\Doctrine\Orm\Filter\OrderFilter::NULLS_LARGEST
(nulls_largest
) strategyApiPlatform\Doctrine\Orm\Filter\OrderFilter::NULLS_ALWAYS_FIRST
(nulls_always_first
) strategyApiPlatform\Doctrine\Orm\Filter\OrderFilter::NULLS_ALWAYS_LAST
(nulls_always_last
) strategyclass ApiPlatform\Doctrine\Orm\Filter\OrderFilter 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/OrderFilterInterface">ApiPlatform\Doctrine\Common\Filter\OrderFilterInterface</a>`
{
public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, string $orderParameterName, null|Psr\Log\LoggerInterface $logger, null|array $properties, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter, null|string $orderNullsComparison)
public apply(Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
protected filterProperty(string $property, $direction, 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<int, string> $associations): Doctrine\Persistence\Mapping\ClassMetadata
protected getClassMetadata(string $resourceClass): Doctrine\Persistence\Mapping\ClassMetadata
public getDescription(string $resourceClass): array
}
class ApiPlatform\Doctrine\Orm\Filter\OrderFilter 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/OrderFilterInterface">ApiPlatform\Doctrine\Common\Filter\OrderFilterInterface</a>`
{
public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, string $orderParameterName, null|Psr\Log\LoggerInterface $logger, null|array $properties, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter, null|string $orderNullsComparison)
public apply(Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
protected filterProperty(string $property, $direction, 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<int, string> $associations): Doctrine\Persistence\Mapping\ClassMetadata
protected getClassMetadata(string $resourceClass): Doctrine\Persistence\Mapping\ClassMetadata
public getDescription(string $resourceClass): array
}
Psr\Log\LoggerInterface $logger
Psr\Log\LoggerInterface $logger
Doctrine\Persistence\ManagerRegistry $managerRegistry
Doctrine\Persistence\ManagerRegistry $managerRegistry
array $properties
array $properties
Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter
Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter
string $orderParameterName
string $orderParameterName
public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, string $orderParameterName, null|Psr\Log\LoggerInterface $logger, null|array $properties, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter, null|string $orderNullsComparison)
public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, string $orderParameterName, null|Psr\Log\LoggerInterface $logger, null|array $properties, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter, null|string $orderNullsComparison)
managerRegistry | Doctrine\Persistence\ManagerRegistry | |
orderParameterName | string | |
logger | Psr\Log\LoggerInterface | |
properties | array | |
nameConverter | Symfony\Component\Serializer\NameConverter\NameConverterInterface | |
orderNullsComparison | string |
Applies the filter.Applies the filter.
public apply(Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
public apply(Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
queryBuilder | Doctrine\ORM\QueryBuilder | |
queryNameGenerator | ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface | |
resourceClass | string | |
operation | ApiPlatform\Metadata\Operation | |
context | array |
null
Passes a property through the filter.
protected filterProperty(string $property, $direction, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
protected filterProperty(string $property, $direction, Doctrine\ORM\QueryBuilder $queryBuilder, ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
property | string | |
direction | ||
queryBuilder | Doctrine\ORM\QueryBuilder | |
queryNameGenerator | ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface | |
resourceClass | string | |
operation | ApiPlatform\Metadata\Operation | |
context | array |
null
Splits the given property into parts.Returns an array with the following keys:
protected splitPropertyParts(string $property, string $resourceClass): array
protected splitPropertyParts(string $property, string $resourceClass): array
property | string | |
resourceClass | string |
array
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
property | string | |
resourceClass | string | |
allowAssociation | bool |
bool
Determines whether the given property is nested.
protected isPropertyNested(string $property, string $resourceClass): bool
protected isPropertyNested(string $property, string $resourceClass): bool
property | string | |
resourceClass | string |
bool
Determines whether the given property is embedded.
protected isPropertyEmbedded(string $property, string $resourceClass): bool
protected isPropertyEmbedded(string $property, string $resourceClass): bool
property | string | |
resourceClass | string |
bool
Gets the Doctrine Type of a given property/resourceClass.
protected getDoctrineFieldType(string $property, string $resourceClass): string
protected getDoctrineFieldType(string $property, string $resourceClass): string
property | string | |
resourceClass | string |
string
Gets nested class metadata for the given resource.
protected getNestedMetadata(string $resourceClass, array<int, string> $associations): Doctrine\Persistence\Mapping\ClassMetadata
protected getNestedMetadata(string $resourceClass, array<int, string> $associations): Doctrine\Persistence\Mapping\ClassMetadata
resourceClass | string | |
associations | array<int, string> |
Doctrine\Persistence\Mapping\ClassMetadata
Gets class metadata for the given resource.
protected getClassMetadata(string $resourceClass): Doctrine\Persistence\Mapping\ClassMetadata
protected getClassMetadata(string $resourceClass): Doctrine\Persistence\Mapping\ClassMetadata
resourceClass | string |
Doctrine\Persistence\Mapping\ClassMetadata
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:
public getDescription(string $resourceClass): array
public getDescription(string $resourceClass): array
resourceClass | string |
array