Guides
Tutorials
The date filter allows to filter a collection by date intervals.
Syntax: ?property[<after|before|strictly_after|strictly_before>]=value
.
The value can take any date format supported by the \DateTime
constructor.
The after
and before
filters will filter including the value whereas strictly_after
and strictly_before
will filter excluding the value.
The date filter is able to deal with date properties having null
values. Four behaviors are available at the property level of the filter:
null
strategyApiPlatform\Doctrine\Odm\Filter\DateFilter::EXCLUDE_NULL
(exclude_null
) strategyApiPlatform\Doctrine\Odm\Filter\DateFilter::INCLUDE_NULL_BEFORE
(include_null_before
) strategyApiPlatform\Doctrine\Odm\Filter\DateFilter::INCLUDE_NULL_AFTER
(include_null_after
) strategyApiPlatform\Doctrine\Odm\Filter\DateFilter::INCLUDE_NULL_BEFORE_AND_AFTER
(include_null_before_and_after
) strategy<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Odm\Filter\DateFilter;
#[ApiResource]
#[ApiFilter(DateFilter::class, properties: ['createdAt'])]
class Book
{
// ...
}
<?php
// api/src/Entity/Book.php
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Doctrine\Odm\Filter\DateFilter;
#[ApiResource]
#[ApiFilter(DateFilter::class, properties: ['createdAt'])]
class Book
{
// ...
}
# config/services.yaml
services:
book.date_filter:
parent: 'api_platform.doctrine.odm.date_filter'
arguments: [ { createdAt: ~ } ]
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.date_filter']
# config/services.yaml
services:
book.date_filter:
parent: 'api_platform.doctrine.odm.date_filter'
arguments: [ { createdAt: ~ } ]
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.date_filter']
<!-- 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.date_filter" parent="api_platform.doctrine.odm.date_filter">
<argument type="collection">
<argument key="createdAt"></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.date_filter</filter>
</filters>
</operation>
</operations>
</resource>
</resources>
<!-- 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.date_filter" parent="api_platform.doctrine.odm.date_filter">
<argument type="collection">
<argument key="createdAt"></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.date_filter</filter>
</filters>
</operation>
</operations>
</resource>
</resources>
Given that the collection endpoint is /books
, you can filter books by date with the following query: /books?createdAt[after]=2018-03-19
.
class ApiPlatform\Doctrine\Odm\Filter\DateFilter 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/DateFilterInterface">ApiPlatform\Doctrine\Common\Filter\DateFilterInterface</a>`
{
protected filterProperty(string $property, $values, Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, 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 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<int, string> $associations): Doctrine\Persistence\Mapping\ClassMetadata
public getDescription(string $resourceClass): array
protected isDateField(string $property, string $resourceClass): bool
protected getFilterDescription(string $property, string $period): array
}
class ApiPlatform\Doctrine\Odm\Filter\DateFilter 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/DateFilterInterface">ApiPlatform\Doctrine\Common\Filter\DateFilterInterface</a>`
{
protected filterProperty(string $property, $values, Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
public __construct(Doctrine\Persistence\ManagerRegistry $managerRegistry, 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 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<int, string> $associations): Doctrine\Persistence\Mapping\ClassMetadata
public getDescription(string $resourceClass): array
protected isDateField(string $property, string $resourceClass): bool
protected getFilterDescription(string $property, string $period): 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
Passes a property through the filter.
protected filterProperty(string $property, $values, Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
protected filterProperty(string $property, $values, Doctrine\ODM\MongoDB\Aggregation\Builder $aggregationBuilder, string $resourceClass, null|ApiPlatform\Metadata\Operation $operation, array $context): null
property | string | |
values | ||
aggregationBuilder | Doctrine\ODM\MongoDB\Aggregation\Builder | |
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
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
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 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
Determines whether the given property refers to a date field.
protected isDateField(string $property, string $resourceClass): bool
protected isDateField(string $property, string $resourceClass): bool
property | string | |
resourceClass | string |
bool
Gets filter description.
protected getFilterDescription(string $property, string $period): array
protected getFilterDescription(string $property, string $period): array
property | string | |
period | string |
array