Reference
C
Class

ApiPlatform\Doctrine\Odm\Filter\OrderFilter

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\Odm\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\Odm\Filter\OrderFilter;
 
#[ApiResource]
#[ApiFilter(OrderFilter::class, properties: ['id', 'title'], arguments: ['orderParameterName' => 'order'])]
class Book
{
    // ...
}

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:

<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Odm\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\Odm\Filter\OrderFilter;
 
#[ApiResource]
#[ApiFilter(OrderFilter::class, properties: ['id' => 'ASC', 'title' => 'DESC'])]
class Book
{
    // ...
}

When the property used for ordering can contain null values, you may want to specify how null values are treated in the comparison:

  • Use the default behavior of the DBMS: use null strategy
  • Exclude items: use ApiPlatform\Doctrine\Odm\Filter\OrderFilter::NULLS_SMALLEST (nulls_smallest) strategy
  • Consider items as oldest: use ApiPlatform\Doctrine\Odm\Filter\OrderFilter::NULLS_LARGEST (nulls_largest) strategy
  • Consider items as youngest: use ApiPlatform\Doctrine\Odm\Filter\OrderFilter::NULLS_ALWAYS_FIRST (nulls_always_first) strategy
  • Always include items: use ApiPlatform\Doctrine\Odm\Filter\OrderFilter::NULLS_ALWAYS_LAST (nulls_always_last) strategy
class ApiPlatform\Doctrine\Odm\Filter\OrderFilter extends ApiPlatform\Doctrine\Odm\Filter\AbstractFilter implements `<a href="/docs/reference/Doctrine/Odm/Filter/FilterInterface">ApiPlatform\Doctrine\Odm\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)
    public apply(Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
    protected filterProperty(string $property, $direction, Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, 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 getClassMetadata(string $resourceClass): Doctrine\Persistence\Mapping\ClassMetadata
    protected addLookupsForNestedProperty(string $property, Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, string $resourceClass): 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
    public getDescription(string $resourceClass): array
}
class ApiPlatform\Doctrine\Odm\Filter\OrderFilter extends ApiPlatform\Doctrine\Odm\Filter\AbstractFilter implements `<a href="/docs/reference/Doctrine/Odm/Filter/FilterInterface">ApiPlatform\Doctrine\Odm\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)
    public apply(Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
    protected filterProperty(string $property, $direction, Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, 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 getClassMetadata(string $resourceClass): Doctrine\Persistence\Mapping\ClassMetadata
    protected addLookupsForNestedProperty(string $property, Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, string $resourceClass): 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
    public getDescription(string $resourceClass): array
}

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

orderParameterName

string $orderParameterName
string $orderParameterName

Methods

__construct

public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, string $orderParameterName, null|Psr\Log\LoggerInterface $logger, null|array $properties, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter)
public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, string $orderParameterName, null|Psr\Log\LoggerInterface $logger, null|array $properties, null|Symfony\Component\Serializer\NameConverter\NameConverterInterface $nameConverter)

Parameters

managerRegistryDoctrine\Persistence\ManagerRegistry
orderParameterNamestring
loggerPsr\Log\LoggerInterface
propertiesarray
nameConverterSymfony\Component\Serializer\NameConverter\NameConverterInterface

apply

Applies the filter.Applies the filter.

public apply(Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
public apply(Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null

Parameters

aggregationBuilderDoctrine\ODM\MongoDB\Aggregation\Builder
resourceClassstring
operationApiPlatform\Metadata\Operation
contextarray

Returns

null

filterProperty

Passes a property through the filter.

protected filterProperty(string $property, $direction, Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
protected filterProperty(string $property, $direction, Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null

Parameters

propertystring
direction
aggregationBuilderDoctrine\ODM\MongoDB\Aggregation\Builder
resourceClassstring
operationApiPlatform\Metadata\Operation
contextarray

Returns

null

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

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

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

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

Copyright © 2023 Kévin Dunglas

Sponsored by Les-Tilleuls.coop