Doctrine features a filter system that allows the developer to add additional
criteria to queries, regardless of where the query is generated within the
application (e.g. from a query builder, loading referenced documents). This is
useful for excluding documents at a low level, to ensure that they are neither
returned from MongoDB nor hydrated by ODM.
Throughout this document, the example
MyLocaleFilter
class will be used to
illustrate how the filter feature works. A filter class must extend the base
Doctrine\ODM\MongoDB\Query\Filter\BsonFilter
class and implement the
addFilterCriteria()
method. This method receives
ClassMetadata
and is
invoked whenever a query is prepared for any class. Since filters are typically
designed with a specific class or interface in mind,
addFilterCriteria()
will frequently start by checking
ClassMetadata
and returning immediately if
it is not supported.
Parameters for the query should be set on the filter object by calling the
BsonFilter::setParameter()
method. Within the filter class, parameters
should be accessed via
BsonFilter::getParameter()
.
<?phpnamespaceVendor\Filter;
useDoctrine\ODM\MongoDB\Mapping\ClassMetadata;
useDoctrine\ODM\MongoDB\Query\Filter\BsonFilter;
classMyLocaleFilterextendsBsonFilterpublicfunctionaddFilterCriteria(ClassMetadata $targetDocument): array// Check if the entity implements the LocalAware interfaceif ( ! $targetDocument->reflClass->implementsInterface('LocaleAware')) {
return [];
return ['locale' => $this->getParameter('locale')];
The Configuration#addFilter() method takes a name for the filter and the
name of the filter class, which will be constructed as necessary.
An optional third parameter may be used to set parameters at configuration time:
Filters can be disabled and enabled via the FilterCollection, which is
stored in the DocumentManager. The FilterCollection#enable($name) method
may be used to enabled and return a filter, after which you may set parameters.
Disabling and enabling filters has no effect on managed documents. If you
want to refresh or reload an object after having modified a filter or the
FilterCollection, then you should clear the DocumentManager and re-fetch
your documents so the new filtering rules may be applied.