-
My need is simple : I would like to attach a <?php
#[QueryParameter(key: ':property', filter: SearchFilter::class)]
class Book {
public string $id;
public string $title;
public Author $author;
} https://api-platform.com/docs/core/filters/#the-property-placeholder So I tried something like that. <?php
#[GetCollection(
class: WrapperEntity::class,
output: WrapperApiResource::class,
provider: State\Provider\WrapperProvider::class,
parameters: [
'makes[]' => new QueryParameter(
'makes[]',
schema: [
'type' => 'array',
'items' => ['type' => 'string'],
],
filter: SearchFilter::class,
description: 'Make of the vehicle',
properties: ['vehicle.make'],
filterContext: [
'properties' => [
'vehicle.make' => 'exact',
],
],
),
]
)]
final class WrapperApiResource With that I thought I'd be able to link my queryParameter I'm a bit lost with that, I don't think it's a use case that complex that would need a custom filter so i wanted to avoid that but have I to make one (or a custom Doctrine Extension) ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
It's quite hard to have working filters for all the use cases and in the end developers often just use a custom filter. We simplified a lot the approach with query parameters, a few fixes to your code:
<?php
#[GetCollection(
// do not use this, please declare the ApiResource on the class you want
// class: WrapperEntity::class,
// This is "okay" but if you should prefer just having this class represent your API
output: WrapperApiResource::class,
provider: State\Provider\WrapperProvider::class,
parameters: [
'makes' => new QueryParameter(
'makes[]',
schema: [
'type' => 'array',
'items' => ['type' => 'string'],
],
filter: SearchFilter::class,
description: 'Make of the vehicle',
),
]
)]
final class WrapperEntity use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
final class SearchFilter implements FilterInterface
{
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
{
$queryBuilder->join('o.vehicle', 'v')->andWhere('v.make = :make')->setParameter('make', $context['parameter']->getValue());
}
// Avoid this, use OpenApiParameterFilterInterface and/or JsonSchemaFilterInterface if you need, by default we use the values on the parameters
public function getDescription(string $resourceClass): array
{
return [];
}
} |
Beta Was this translation helpful? Give feedback.
Short answer yes. Defining an ApiFilter does two things: declare a filter Symfony service, add documentation.
The Parameter is a metadata-first approach and the filter is basically a simple callable (like I showed earlier). It's still compatible with services but as our current filters need some mandatory arguments it's hard to make them compatible (especially for the SearchFilter, see linked PR).
I'm sorry for the lack of documentation, best is to take a look at https://github.com/api-platform/core/tree/main/tests/Functional/Parameters and https://github.com/api-platform/core/blob/main/…