Filters ======= .. versionadded:: 2.2 Doctrine 2.2 features a filter system that allows the developer to add SQL to the conditional clauses of queries, regardless the place where the SQL is generated (e.g. from a DQL query, or by loading associated entities). The filter functionality works on SQL level. Whether a SQL query is generated in a Persister, during lazy loading, in extra lazy collections or from DQL. Each time the system iterates over all the enabled filters, adding a new SQL part as a filter returns. By adding SQL to the conditional clauses of queries, the filter system filters out rows belonging to the entities at the level of the SQL result set. This means that the filtered entities are never hydrated (which can be expensive). Example filter class -------------------- 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\ORM\Query\Filter\SQLFilter`` class and implement the ``addFilterConstraint`` method. The method receives the ``ClassMetadata`` of the filtered entity and the table alias of the SQL table of the entity. .. note:: In the case of joined or single table inheritance, you always get passed the ClassMetadata of the inheritance root. This is necessary to avoid edge cases that would break the SQL when applying the filters. Parameters for the query should be set on the filter object by ``SQLFilter#setParameter()``. Only parameters set via this function can be used in filters. The ``SQLFilter#getParameter()`` function takes care of the proper quoting of parameters. .. code-block:: php <?php namespace Example; use Doctrine\ORM\Mapping\ClassMetadata, Doctrine\ORM\Query\Filter\SQLFilter; class MyLocaleFilter extends SQLFilter { public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias) { // Check if the entity implements the LocalAware interface if (!$targetEntity->reflClass->implementsInterface('LocaleAware')) { return ""; } return $targetTableAlias.'.locale = ' . $this->getParameter('locale'); // getParameter applies quoting automatically } } Configuration ------------- Filter classes are added to the configuration as following: .. code-block:: php <?php $config->addFilter("locale", "\Doctrine\Tests\ORM\Functional\MyLocaleFilter"); The ``Configuration#addFilter()`` method takes a name for the filter and the name of the class responsible for the actual filtering. Disabling/Enabling Filters and Setting Parameters --------------------------------------------------- Filters can be disabled and enabled via the ``FilterCollection`` which is stored in the ``EntityManager``. The ``FilterCollection#enable($name)`` method will retrieve the filter object. You can set the filter parameters on that object. .. code-block:: php <?php $filter = $em->getFilters()->enable("locale"); $filter->setParameter('locale', 'en'); // Disable it $filter = $em->getFilters()->disable("locale"); .. warning:: Disabling and enabling filters has no effect on managed entities. If you want to refresh or reload an object after having modified a filter or the FilterCollection, then you should clear the EntityManager and re-fetch your entities, having the new rules for filtering applied.
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
advanced-configuration.rst | File | 16.18 KB | 0644 |
|
annotations-reference.rst | File | 37.16 KB | 0644 |
|
architecture.rst | File | 7.52 KB | 0644 |
|
association-mapping.rst | File | 31.32 KB | 0644 |
|
basic-mapping.rst | File | 17.24 KB | 0644 |
|
batch-processing.rst | File | 5.77 KB | 0644 |
|
best-practices.rst | File | 3.61 KB | 0644 |
|
caching.rst | File | 13.24 KB | 0644 |
|
change-tracking-policies.rst | File | 5.13 KB | 0644 |
|
configuration.rst | File | 4.29 KB | 0644 |
|
dql-doctrine-query-language.rst | File | 60.95 KB | 0644 |
|
events.rst | File | 30.98 KB | 0644 |
|
faq.rst | File | 9.95 KB | 0644 |
|
filters.rst | File | 3.38 KB | 0644 |
|
improving-performance.rst | File | 2.66 KB | 0644 |
|
inheritance-mapping.rst | File | 20.79 KB | 0644 |
|
installation.rst | File | 131 B | 0644 |
|
limitations-and-known-issues.rst | File | 7.66 KB | 0644 |
|
metadata-drivers.rst | File | 6.02 KB | 0644 |
|
namingstrategy.rst | File | 4.38 KB | 0644 |
|
native-sql.rst | File | 34.31 KB | 0644 |
|
partial-objects.rst | File | 3.52 KB | 0644 |
|
php-mapping.rst | File | 8.82 KB | 0644 |
|
query-builder.rst | File | 20.6 KB | 0644 |
|
second-level-cache.rst | File | 24.02 KB | 0644 |
|
security.rst | File | 4.68 KB | 0644 |
|
tools.rst | File | 16.8 KB | 0644 |
|
transactions-and-concurrency.rst | File | 13.9 KB | 0644 |
|
unitofwork-associations.rst | File | 2.74 KB | 0644 |
|
unitofwork.rst | File | 6.73 KB | 0644 |
|
working-with-associations.rst | File | 22.24 KB | 0644 |
|
working-with-objects.rst | File | 31.61 KB | 0644 |
|
xml-mapping.rst | File | 27.11 KB | 0644 |
|
yaml-mapping.rst | File | 4.42 KB | 0644 |
|