Symfony 2.1原理过滤器(启用/禁用) [英] Symfony 2.1 Doctrine filters (enable/disable)
问题描述
我目前在我的Symfony2中实施Doctrine 过滤器 .1项目,具有以下设置:
I'm currently implementing Doctrine filters in my Symfony2.1 project with the following setup:
<?php
namespace Acme\Bundle\Entity;
class Article {
/**
* @ORM\Column(type="string")
*/
private $status;
...
}
//app/config/config.yml
doctrine:
orm:
filters:
status:
class: Acme\Bundle\Filter\StatusFilter
enabled: false
....
//src/Acme/Bundle/Filter/StatusFilter.php
namespace Acme\Bundle\Filter;
use Acme\Bundle\Entity\Status;
class StatusFilter extends SQLFilter {
public function addFilterConstraint(ClassMetadata $target, $alias)
{
$filter =
$target->reflClass->implementsInterface('Acme\Bundle\Entity\Status')?
$alias . '.status = ' . Status::PUBLISHED : '';
return $filter;
}
}
其中Acme\Bundle\Entity\Status只是一个接口。
当在 config.yml
中启用过滤器时,代码正常工作。
Where Acme\Bundle\Entity\Status is just an interface.
The code is working as expected when the filter is enabled in config.yml
.
问题是我无法检索所有用于管理的文章!
是否可以为某个捆绑包启用此过滤器?
ps我知道如何使用EntityManager启用和禁用过滤器,
我找不到合适的地方为前端Bundle。
The problem is that I cannot retrieve all articles for administration!
Is there a way to enable this filter for a certain bundle?
p.s. I know how to enable and disable the filter with the EntityManager,
I just cannot find the proper place to do it for the frontend Bundle.
我的管理区段可通过路由前缀 myadmin
my admin section is accessible by route prefix myadmin
www.example.com/myadmin/ - > admin section =禁用过滤器(默认情况下在配置中禁用)
www.example.com / ... - > anything else =启用过滤器。
www.example.com/myadmin/ -> admin section = disable filter (disabled by default in config) www.example.com/... -> anything else = enable filter.
推荐答案
在Doctrine级别没有bundle的概念。我看到的唯一方法是通过在 kernel.request
事件期间解析其className(reflect,...)来检测使用哪个控制器,或者通过 kernel.controller
事件。
there is no notion of bundle at Doctrine level. The only way I see would be to detect which controller is used, by parsing its className (reflection, ...) during a kernel.request
event, or a kernel.controller
event.
您可以从这个例子中启发自己:
You can inspire yourself from this example:
然后,如果您检测到您的控制器位于 FrontendBundle
Then, if you detect that your controller is in FrontendBundle
, just disable/enable your doctrine filter.
如果你喜欢使用路由来检测何时禁用/启用,只需使用 kernel.request
事件。您可以通过 $ event-> getRequest() - > attributes-> get('_ controller')
访问所有请求参数。
If you prefer using routing to detect when to disable/enable, just use kernel.request
event. You will have access to all request parameters, via $event->getRequest()->attributes->get('_controller')
for example.
这篇关于Symfony 2.1原理过滤器(启用/禁用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!