使用EasyAdminBundle查询的DQL过滤器

时间:2018-10-11 12:49:52

标签: symfony dql symfony2-easyadmin easyadmin

我有一个具有属性Page的学说实体category。我使用EasyAdminBundle修改页面。有4个固定类别,我希望每个类别都有一个单独的列表视图。在菜单栏中,每个类别应单独显示。

我知道我可以通过使用相同的学说类创建单独的实体并为每个实体提供单独的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的过滤器,但是我不知道如何从菜单配置中进行过滤。有人知道对此可能的解决方案吗?谢谢!

1 个答案:

答案 0 :(得分:1)

不幸的是,没有解决方案。哈维尔·埃吉卢兹(Javier Eguiluz)在Github上回答了我:

  

恐怕这是使用YAML进行配置的缺点之一。对于这样的事情,重用内容/配置不是一件容易的事。我们不打算增加对此功能的支持。虽然这不是理想的解决方案,但我建议您复制并粘贴四个类别的配置。抱歉!

     

https://github.com/EasyCorp/EasyAdminBundle/issues/2386