From 0b351776efcc6d46801f0302162cf5ea908bf25a Mon Sep 17 00:00:00 2001 From: soyuka Date: Wed, 25 Nov 2026 10:23:27 +0100 Subject: [PATCH] docs(doctrine): document SubtypeFilter --- core/doctrine-filters.md | 99 ++++++++++++++++++++++++++++++++++++++++ core/filters.md | 5 ++ 2 files changed, 104 insertions(+) diff --git a/core/doctrine-filters.md b/core/doctrine-filters.md index 22d8a389cd3..e537622e9a6 100644 --- a/core/doctrine-filters.md +++ b/core/doctrine-filters.md @@ -1065,6 +1065,105 @@ itself, wrapping a legacy filter in a `ChainFilter` also suppresses the cosmetic to referencing the filter by service id when you need to compose it with another filter on the same key. +## Subtype Filter + +> [!NOTE] Since API Platform 5.1, `SubtypeFilter` is available for Doctrine ORM +> (`ApiPlatform\Doctrine\Orm\Filter\SubtypeFilter`). It is not available for MongoDB ODM. + +When a resource is backed by a Doctrine inheritance hierarchy (single table or class table), a +property declared on a subclass cannot be referenced in DQL from the parent entity. `SubtypeFilter` +is a decorator that solves this: it applies the filter it wraps to the rows of a given subtype only. + +The `QueryParameter`'s `property` is the property of the subtype. Internally the filter generates: + +```sql +root INSTANCE OF Subtype AND root.id IN (SELECT sub.id FROM Subtype sub WHERE ) +``` + +Constructor: `new SubtypeFilter(string $subtype, FilterInterface $filter, bool $strict = true)`. +`$subtype` is an entity, a mapped superclass or an interface. It is expanded to the topmost mapped +entities of the queried hierarchy that are instances of it, so a mapped superclass or an interface +matches every entity that extends or implements it. New subclasses are picked up automatically. If +no mapped entity matches, a `RuntimeException` is thrown. + +```php + new QueryParameter( + filter: new SubtypeFilter(Car::class, new ExactFilter()), + property: 'seats', + ), + 'seatsOrOtherType' => new QueryParameter( + filter: new SubtypeFilter(Car::class, new ExactFilter(), strict: false), + property: 'seats', + ), + 'horsepower' => new QueryParameter( + filter: new SubtypeFilter(MotorVehicle::class, new ComparisonFilter(new ExactFilter())), + property: 'horsepower', + ), + ]), +])] +#[ORM\Entity] +#[ORM\InheritanceType('SINGLE_TABLE')] +#[ORM\DiscriminatorColumn(name: 'type')] +#[ORM\DiscriminatorMap([ + 'car' => Car::class, + 'sports_car' => SportsCar::class, + 'truck' => Truck::class, + 'bicycle' => Bicycle::class, +])] +abstract class Vehicle +{ + // id, name +} +``` + +Here `Car` declares `seats`, and `MotorVehicle` is a mapped superclass declaring `horsepower` that +`Car`, `SportsCar` and `Truck` extend. + +The query parameter has the shape of the decorated filter: + +- `/vehicles?seats=4` — cars with 4 seats, including subclasses of `Car` such as `SportsCar`. The + array form `?seats[]=4&seats[]=2` is also supported, as `ExactFilter` does. +- `/vehicles?horsepower[gte]=300&horsepower[lt]=400` — every motor vehicle with at least 300 and + fewer than 400 horsepower. + +### Strict and Non-Strict Mode + +With `strict: true` (the default), only rows of the subtype that match the condition are returned. + +With `strict: false`, the result contains the rows of the subtype that match, plus every row of +other types, as the condition does not apply to them. `/vehicles?seatsOrOtherType=4` returns the +cars with 4 seats and all the vehicles that are not cars. + +### Using SubtypeFilter in an OrFilter + +Inside an `OrFilter`, `SubtypeFilter` always behaves as strict. A non-strict condition would match +every row of other types and make the whole disjunction match everything. + +### OpenAPI and JSON Schema + +The OpenAPI parameter and the JSON Schema come from the decorated filter. + +### Limitations + +- Composite identifiers are not supported. +- Only properties declared on the subtype itself are supported. Nested paths such as `owner.name` + are not. +- Doctrine ORM only. There is no MongoDB ODM implementation. + ## Boolean Filter > [!TIP] Consider using [`ExactFilter`](#exact-filter) as a modern replacement. `ExactFilter` does diff --git a/core/filters.md b/core/filters.md index 07ff9d5fc99..800e5655ac4 100644 --- a/core/filters.md +++ b/core/filters.md @@ -84,6 +84,11 @@ a new instance: [Doctrine Filters documentation](doctrine-filters.md#chain-filter) for details. - Usage: `new QueryParameter(filter: new ChainFilter([new ExactFilter(), new DateFilter()]), property: 'birthdate')` +- **`SubtypeFilter`** (Doctrine ORM only, since API Platform 5.1): Decorates a filter to apply it to + a property declared on a subclass of a Doctrine inheritance hierarchy. See the + [Doctrine Filters documentation](doctrine-filters.md#subtype-filter) for details. + - Usage: + `new QueryParameter(filter: new SubtypeFilter(Car::class, new ExactFilter()), property: 'seats')` - **`BooleanFilter`**: For boolean field filtering (legacy, `ExactFilter` is recommended instead). - Usage: `new QueryParameter(filter: BooleanFilter::class)` - **`NumericFilter`**: For numeric field filtering (legacy, `ExactFilter` or `ComparisonFilter` is