1

我有一个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 的过滤器,但我不知道如何从菜单配置中执行此操作。有人知道可能的解决方案吗?谢谢!

4

1 回答 1

2

不幸的是,没有解决办法。Javier Eguiluz 在 Github 上回答我:

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

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

于 2018-10-13T10:42:51.120 回答