How to setup and use doctrine filters with Symfony 2?

If we want to use some doctrine extensions, it could be necessary to configure filters first. For example soft-deletable (More about extensions here)
In your config in yml, you must find doctrine section:

orm:
    [....]
    entity_managers:
        default:
        [.....]
            filters:
                soft-deleteable:
                    class:    Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                    enabled:  true

This made plugin avaible for use, and enabled by default.
If you want to manually decide if filter is used for query, You can use function enable/disable on entityManager:

    //Disable filter
    $em->getFilters()->disable('soft_deleteable');
    //Made a query here 
    //Enable filter
    $em->getFilters()->enable('soft_deleteable');

Note that in enable/disable function for SoftDeleteableFilter You have to use name with underscore: soft_deleteable.
Secondly it’s very important to remember to enable the filter again, after You made a query

Common mistakes:

1. Put filters directly under doctrine section, not orm, which end with error like this:

Unrecognized options “filters” under “doctrine.orm”

2. Use – instead of _ in disable/enable function,
which will display this error:

Filter ‘soft-deleteable’ is not enabled.

3. Disable the same filter few times in one request without enableing it.
As I wrote above. After You made a query with disabled filter You should enable it again. This could make problem with others query (which should have the filter enabled) or entity manager could try to disable, already disabled filter, which throw an Exception:

Filter ‘soft_deleteable’ is not enabled.

  • PARFAIT ONANA

    po

Still not convinced?

Contact Us and tell what You need.

We will prepare a competitive offer and send you examples of our previous projects.

Twitter
  • Tweets not available at this time. Please Try again later
Toys we play with
Get in touch
  • Address:

    Nextima Sp. z o.o. ul. Terespolska 4 / 375 03813 WARSAW, POLAND

  • Email

    contact@nextima.com

  • Phone

    (+48) 501 32 40 41