2

I have an array of entities (groups) in an entity (user), and in my Query Builder I would like to do something like this:

$groups = $current_user->getGroups();
$usersQuery = $em->getRepository('AppBundle:Users')->createQueryBuilder('u');

foreach ($groups as $group) {
    $usersQuery
        ->orWhere(':group_value in (u.groups)')
        ->setParameter('group_value', $group);
}

But doctrine doesn't like it. Is there another way to check if a value is in a SQL array ?

Thanks.

1 Answer 1

2

Use MEMBER OF DQL statement for these cases:

foreach ($groups as $group) {
    $usersQuery
        ->orWhere(':group_value MEMBER OF u.groups')
        ->setParameter('group_value', $group);
}

Definition: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#collection-expressions

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.