Search code examples
symfonyeasyadmin

EasyAdmin list action for current user


I'm trying to retrieve a list of objects from database where Entity.user = "current user".

It's a ManyToOne relation between an Entity entity and a User Entity

I tried to use the dql-filter option in the bundle config but couldn't find a parameter variable like we can find in Controller $this->getUser() or in Twig {{app.user}}

I tried to use custom controller but I am confused as the documentation is not very detailed.


Solution

  • I would go with a custom controller and overwrite findAll or createListQueryBuilder by always adding a DQL-filter. Something a bit like this:

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