简体   繁体   中英

Sonata Admin Bundle list users of a group

I have Sonata Admin Bundle and Sonata User Bundle (FOSUserBundle with users and groups).

In UserAdmin.php I need to modify the list query to show only the user of a specific group.

I tried with

public function createQuery($context = 'list')
    {
        $query = parent::createQuery($context);
        $query->andWhere(
            $query->expr()->eq($query->getRootAliases()[0] . '.groups', ':my_param')
        );
        $query->setParameter('my_param', '9'); //9 is the id of a group
        return $query;
    }

But I have

[Semantical Error] line 0, col 72 near 'groups = :my': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.

I've found the solution, the correct query is:

public function createQuery($context = 'list')
    {
        $query = parent::createQuery($context);
        $query->andWhere(
            $query->expr()->isMemberOf(':groupId', $query->getRootAliases()[0] . '.groups')
        );
        $query->setParameter('groupId', '9'); //9 is the id of a group


        return $query;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM