Symfony 2.1 Doctrine过滤器(启用/禁用) [英] 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启用和禁用过滤器,
我只是找不到正确的地方去做前端捆绑。
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 / ... - >其他=启用过滤器。
www.example.com/myadmin/ -> admin section = disable filter (disabled by default in config) www.example.com/... -> anything else = enable filter.
推荐答案
在Doctrine级别没有束缚的概念。我看到的唯一方法是通过在 kernel.request
事件或<$ c中解析其className(反射,...)来检测使用哪个控制器$ c> kernel.controller 事件。
您可以从这个例子中激发自己:
You can inspire yourself from this example:
然后,如果您检测到您的控制器位于 FrontendBundle
只需禁用/启用您的教义过滤器。
Then, if you detect that your controller is in FrontendBundle
, just disable/enable your doctrine filter.
如果您希望使用路由来检测何时禁用/启用,只需使用内核.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 Doctrine过滤器(启用/禁用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!