有没有办法访问SQLFilter中的symfony2容器? [英] Is there a way to access the symfony2 container within an SQLFilter?

查看:125
本文介绍了有没有办法访问SQLFilter中的symfony2容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可能在SQLFilter中获取symfony2的服务容器,或者我可以直接使用一个服务作为SQLFilter?



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

解决方案

它不干净,但可以尝试这样:

 <?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);
}
}


is there any possibility to get the service-container of symfony2 within an SQLFilter or can i maybe directly use a service as SQLFilter?

I know that this isn't a "clean" way, but i have to perform several checks directly before the final submit of the query gets fired (as i have to append conditions to the WHERE-statement, i can't use lifecycle-events at this point).

解决方案

it's not clean but you could try this:

<?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);
}
}

这篇关于有没有办法访问SQLFilter中的symfony2容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆