DAnnebicque
DAnnebicque

Reputation: 47

How to filter list with relation ManyToMany

I try to use EasyAdmin, which looks very wonderful. I would like to make this, but I don't find How.

I have some "projects", which can have many "managers" (I have a ManyToMany relationship between these two entities). When a manager logs in with EasyAdmin, I would like to list only his projects in the list of projects. I try to use the filters, but I do not see what to put as a constraint to have only my user's projects.

Do you have an idea? Is it possible to do that?

easy_admin:
    site_name: 'PTUT MMI'
    list:
        actions: ['show']
    formats:
        date:     'd/m/Y'
        time:     'H:i'
        datetime: 'd/m/Y H:i:s'
    user:
        display_name: true
        display_avatar: false
    design:
        menu:
            # only users with the ROLE_SUPER_ADMIN role will see this menu item
            - { entity: 'Projet', permission: ['ROLE_ADMIN',] }
            - { icon: user-cog, label: 'Mes Projets', entity: ProjetsResponsable, params: { action: list, filters: { responsables: { comparison: like, value: [1] } } } }
            - { entity: 'User', permission: ['ROLE_ADMIN'] }
            - { entity: 'Etudiant', permission: ['ROLE_RESPONSABLE'] }
    entities:
#        # List the entity class name you want to manage
        ## Entités pour l'admin
        Projet:
            class: App\Entity\Projet
        User:
            class: App\Entity\User
        ## Entités pour le responsable
        ProjetsResponsable:
            class: App\Entity\Projet
            list:
                filters:
                    - { property: responsables, type: array }
        Etudiant:
            class: App\Entity\User

I have try to transform Responsables (manager) in array, it is a collection, and filter with like in menu (with 1 for the example, but normaly with the connected user).

Thanks,

Upvotes: 1

Views: 1905

Answers (1)

Evgeny Ruban
Evgeny Ruban

Reputation: 1443

some useful information you can get from the documentation.

So, I have done similar task creating custom Controller and overriding createListQueryBuilder method to get custom list for each user. So, in your example it will be something like this:

class ProjetsResponsable extends BaseEasyAdminController
{
    protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
    {
        if (null === $dqlFilter) {
            $dqlFilter = sprintf('entity.user = %s', $this->getUser()->getId());
        } else {
            $dqlFilter .= sprintf(' AND entity.user = %s', $this->getUser()->getId());
        }

        return parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
    }
}

And then in the config:

entities:
    ProjetsResponsable:
        class: App\Entity\Projet
        controller: App\Controller\ProjetsResponsable
        list:
            fields:
                #fields...

Upvotes: 4

Related Questions