Search code examples
symfonydqlsymfony2-easyadmineasyadmin

DQL filter in query with EasyAdminBundle


I have a doctrine entity Page that has a property category. I use EasyAdminBundle to modify the pages. There are 4 fixed categories and I want a separate list view for each category. In the menu bar, each category should be displayed separately.

I know I can accomplish this by creating separate entities with the same doctrine class and giving them each a separate DQL filter, like this:

YAML config:

easy_admin:
    design:
        menu:
        -   label: "Pages"
            children:
            -   entity: PageCategory1
            -   entity: PageCategory2
            -   entity: PageCategory3
            -   entity: PageCategory4

However, I don't want to copy and paste all of the other config to each entity every time I change something. I would like to do something like this:

YAML config:

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'"

Right now, I've tried the following. It works to some extent, but when I start searching inside the list view for example, the filter doesn't work anymore. I also think it's not the most elegant solution.

YAML config:

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'"

Override of the listAction method:

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));
}

I see that the EsayAdminExtensionBundle has filters that can be added to the URL, but I have no idea on how to do that from the menu config. Does anybody know a possible solution to this? Thanks!


Solution

  • Unforunately, there is no solution. Javier Eguiluz answered me on 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!

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