Scenario
Doctrine filter is a very powerfull tool that can be used to add conditional clauses at SQL level into our Doctrine 2 engine. This means that filters constraints will affect DQL queries, collections, lazy loading, etc.
How to setup and use filters in generic conditions is well explained in this article of official Doctrine documentation, but in a Zend Framework 2 project, the same operation is a bit different.
So, in this post, we will explain how to configure one or more filters into our Zend Framework 2 projects.
Explicative example
Following example is based on “locale” filter one shown in Doctrine reference. We will assume you just have a working ZF2 project based on ZendSkeletonApplication with a configured DoctrineORMModule (refer to official documentations to reach this achievement).
First of all, we need an interface class to use as mark to decide which entities will be affected by filter. So, if we want that a filter is applied to an entity we only need to make it implements this interface. Of course, we can also use interface to specify methods to implement into entity, if we want. This can be usefull and we will see an example in a future post.
/module/Application/src/Application/DoctrineFilter/MyLocaleFilterInterface.php:
|
|
Essentially, filter definition is identical to Doctrine only usage. We only need to pay attention to file position and namespace adjustments as follow:
/module/Application/src/Application/DoctrineFilter/MyLocaleFilter.php:
|
|
At this point, real differences comes: to setup filter, we will use ZF2 module configuration file instead of addFilter() method (as described in official reference). So let’s add following lines into module.config.php:
/module/Application/config/module.config.php:
|
|
Now that our new filter is configured, we can enable and disable it from any class that implements ServiceManagerAwareInterface interface, adding to it two simple methods like these:
|
|
If we what to enable filter during bootstrap, instead, we can add following piece of code into Module.php file:
/module/Application/Module.php:
|
|
That’s all. From now, while “my_locale” filter is enabled, each entity involved into query or collection loading, will be filtered with condition(s) specified into filter class and values passed by setParameter() method. With few lines of code, we can have filters completely itegrated into ZF2 project.
See also
- How to setup Docker container with legacy PHP 5.6 and Xdebug
- Split, reduce and convert PDF to JPEG using PHP ImageMagick
- Quickly setup HTTPS on PHP Apache2 Docker container with self-signed SSL certificate
- Building ZF3 composed pages using Nesting View Models and Forward Controller Plugin
- How to inject Zend Service Manager in ZF3 Controllers