添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
胡子拉碴的豆腐  ·  Mongoose URI is ...·  1 月前    · 
逆袭的鸭蛋  ·  Delete Documents - ...·  1 月前    · 
多情的仙人球  ·  [Answered] How does ...·  3 周前    · 
礼貌的稀饭  ·  ⚙️ Customize Spring ...·  3 周前    · 
奋斗的木瓜  ·  Create DBRefs in MongoDB·  3 周前    · 
拉风的剪刀  ·  tls1.2 handshake · ...·  4 月前    · 
不羁的高山  ·  Spring Framework Bean ...·  5 月前    · 
近视的香菜  ·  Why does Certbot Use ...·  7 月前    · 

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() .

1<?php namespace Vendor\Filter; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\Query\Filter\BsonFilter; class MyLocaleFilter extends BsonFilter public function addFilterCriteria(ClassMetadata $targetDocument): array // Check if the entity implements the LocalAware interface if ( ! $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.

1<?php $config->addFilter('locale', \Vendor\Filter\MyLocaleFilter::class, ['locale' => 'en']);

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.

1<?php $filter = $dm->getFilterCollection()->enable("locale"); $filter->setParameter('locale', ['$in' => ['en', 'fr']]); // Disable the filter (perhaps temporarily to run an unfiltered query) $filter = $dm->getFilterCollection()->disable("locale");