1
0
mirror of synced 2025-01-18 22:41:43 +03:00

General information on how the filter work etc + warning on the enabling/disabling part

This commit is contained in:
Alexander 2012-01-31 08:49:16 +01:00
parent 7c8a9c0f9a
commit cfbfac6a51

View File

@ -3,17 +3,32 @@ Filters
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). To give
you an idea on how it works, this chapter starts with an example of a filter.
generated (e.g. from a DQL query, or by loading associated entities).
The filter functionality works on SQL level. Whether an 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).
To give you an idea on how it works, the next section contains an example of a
filter.
Example filter class
--------------------
Throughout this document the example ``MyLocaleFilter`` class will be used to
illustrate how the filter feature works. A filter class should 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 table of the entity.
method. The method receives the ``ClassMetadata`` of the filtered entity and the
table alias of the SQL table of the entity.
Parameters for the query should be set on the filter object by
``SQLFilter::setParameter()``. Only parameters set via this function used in
the filters. The ``SQLFilter::getParameter()`` function takes care of the
proper quoting of parameters.
.. code-block:: php
<?php
@ -30,13 +45,11 @@ table alias of the table of the entity.
return "";
}
return $targetTableAlias.'.locale = ' . $this->getParameter('locale');
return $targetTableAlias.'.locale = ' . $this->getParameter('locale'); // Automatically quoted
}
}
Configuration
-------------
Filter classes are added to the configuration as following:
@ -61,6 +74,11 @@ object can be used to set certain parameters for the filter.
$filter = $em->getFilters()->enable("locale");
$filter->setParameter('locale', 'en');
.. warning::
Disabling and enabling filters does not have effect on objects that you
already have. If you want to reload an object after you disabled, enabled
or changed a filter, then you should clear the EM and re-fetch the object
so the appropriate SQL will be executed.
Disabling Filters
-----------------