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