使用 EasyAdminBundle 查询的 DQL 过滤器
DQL filter in query with EasyAdminBundle
我有一个学说实体 Page
,它有一个 属性 category
。我使用 EasyAdminBundle 来修改页面。有 4 个固定类别,我希望每个类别都有一个单独的列表视图。在菜单栏中,每个类别应该单独显示。
我知道我可以通过创建具有相同学说的单独实体 class 并为每个实体提供单独的 DQL 过滤器来实现此目的,如下所示:
YAML 配置:
easy_admin:
design:
menu:
- label: "Pages"
children:
- entity: PageCategory1
- entity: PageCategory2
- entity: PageCategory3
- entity: PageCategory4
但是,我不想每次更改某些内容时都将所有其他配置复制并粘贴到每个实体。我想做这样的事情:
YAML 配置:
easy_admin:
design:
menu:
- label: "Pages"
children:
- entity: Page
label: 'Category 1'
dql_filter: "entity.category = 'category1'"
- entity: Page
label: 'Category 2'
dql_filter: "entity.category = 'category2'"
- entity: Page
label: 'Category 3'
dql_filter: "entity.category = 'category3'"
- entity: Page
label: 'Category 4'
dql_filter: "entity.category = 'category4'"
现在,我尝试了以下方法。它在某种程度上起作用,但是当我开始在列表视图中搜索时,过滤器不再起作用。我也认为这不是最优雅的解决方案。
YAML 配置:
easy_admin:
design:
menu:
- label: "Pages"
children:
- entity: Page
label: 'Category 1'
params:
- dql_filter: "entity.category = 'category1'"
- entity: Page
label: 'Category 2'
params:
- dql_filter: "entity.category = 'category2'"
- entity: Page
label: 'Category 3'
params:
- dql_filter: "entity.category = 'category3'"
- entity: Page
label: 'Category 4'
params:
- dql_filter: "entity.category = 'category4'"
覆盖 listAction 方法:
protected function listAction()
{
$this->dispatch(EasyAdminEvents::PRE_LIST);
$fields = $this->entity['list']['fields'];
/* START CUSTOM PART */
// Check if there is a DQL filter given with the request, otherwise get the DQL filter from the entity itself
$dqlFilter = ($this->request->query->get('0')['dql_filter'] ? $this->request->query->get('0')['dql_filter'] : $this->entity['list']['dql_filter']);
$paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), $this->entity['list']['max_results'], $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $dqlFilter);
/* END CUSTOM PART */
$this->dispatch(EasyAdminEvents::POST_LIST, array('paginator' => $paginator));
$parameters = array(
'paginator' => $paginator,
'fields' => $fields,
'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
);
return $this->executeDynamicMethod('render<EntityName>Template', array('list', $this->entity['templates']['list'], $parameters));
}
我看到 EsayAdminExtensionBundle 有可以添加到 URL 的过滤器,但我不知道如何从菜单配置中做到这一点。有人知道可能的解决方案吗?谢谢!
很遗憾,没有解决办法。 Javier Eguiluz 在 Github 上回答了我:
I'm afraid this is one of the drawbacks of using YAML for config. It's not easy to reuse contents/confgis for things like this. We don't plan to add support for this feature. Although is a solution far from ideal, I recommend you to copy+paste the config for the four categories. I'm sorry!
我有一个学说实体 Page
,它有一个 属性 category
。我使用 EasyAdminBundle 来修改页面。有 4 个固定类别,我希望每个类别都有一个单独的列表视图。在菜单栏中,每个类别应该单独显示。
我知道我可以通过创建具有相同学说的单独实体 class 并为每个实体提供单独的 DQL 过滤器来实现此目的,如下所示:
YAML 配置:
easy_admin:
design:
menu:
- label: "Pages"
children:
- entity: PageCategory1
- entity: PageCategory2
- entity: PageCategory3
- entity: PageCategory4
但是,我不想每次更改某些内容时都将所有其他配置复制并粘贴到每个实体。我想做这样的事情:
YAML 配置:
easy_admin:
design:
menu:
- label: "Pages"
children:
- entity: Page
label: 'Category 1'
dql_filter: "entity.category = 'category1'"
- entity: Page
label: 'Category 2'
dql_filter: "entity.category = 'category2'"
- entity: Page
label: 'Category 3'
dql_filter: "entity.category = 'category3'"
- entity: Page
label: 'Category 4'
dql_filter: "entity.category = 'category4'"
现在,我尝试了以下方法。它在某种程度上起作用,但是当我开始在列表视图中搜索时,过滤器不再起作用。我也认为这不是最优雅的解决方案。
YAML 配置:
easy_admin:
design:
menu:
- label: "Pages"
children:
- entity: Page
label: 'Category 1'
params:
- dql_filter: "entity.category = 'category1'"
- entity: Page
label: 'Category 2'
params:
- dql_filter: "entity.category = 'category2'"
- entity: Page
label: 'Category 3'
params:
- dql_filter: "entity.category = 'category3'"
- entity: Page
label: 'Category 4'
params:
- dql_filter: "entity.category = 'category4'"
覆盖 listAction 方法:
protected function listAction()
{
$this->dispatch(EasyAdminEvents::PRE_LIST);
$fields = $this->entity['list']['fields'];
/* START CUSTOM PART */
// Check if there is a DQL filter given with the request, otherwise get the DQL filter from the entity itself
$dqlFilter = ($this->request->query->get('0')['dql_filter'] ? $this->request->query->get('0')['dql_filter'] : $this->entity['list']['dql_filter']);
$paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), $this->entity['list']['max_results'], $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $dqlFilter);
/* END CUSTOM PART */
$this->dispatch(EasyAdminEvents::POST_LIST, array('paginator' => $paginator));
$parameters = array(
'paginator' => $paginator,
'fields' => $fields,
'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
);
return $this->executeDynamicMethod('render<EntityName>Template', array('list', $this->entity['templates']['list'], $parameters));
}
我看到 EsayAdminExtensionBundle 有可以添加到 URL 的过滤器,但我不知道如何从菜单配置中做到这一点。有人知道可能的解决方案吗?谢谢!
很遗憾,没有解决办法。 Javier Eguiluz 在 Github 上回答了我:
I'm afraid this is one of the drawbacks of using YAML for config. It's not easy to reuse contents/confgis for things like this. We don't plan to add support for this feature. Although is a solution far from ideal, I recommend you to copy+paste the config for the four categories. I'm sorry!