有没有办法在SQLFilter中访问symfony2容器?

swe*_*enb 2 service doctrine dependency-injection symfony doctrine-orm

是否有可能在SQLFilter中获取symfony2的服务容器,或者我可以直接将服务用作SQLFilter?

我知道这不是一个"干净"的方式,但我必须在查询的最终提交被触发之前直接执行几个检查(因为我必须在WHERE语句中附加条件,我不能使用生命周期 - 此时的事件).

Flo*_*ian 6

它不干净但你可以试试这个:

<?php

class MyBundle extends Bundle 
{
public function boot()
{
    $em = $this->container->get('doctrine.orm.default_entity_manager');
    $conf = $em->getConfiguration();
    $conf->addFilter(
        'test',
        'Doctrine\Filter\TestFilter'
    );

    $em->getFilters()->enable('test')->setContainer($this->container);
}
}
Run Code Online (Sandbox Code Playgroud)