Search code examples
phpsymfonydoctrine-ormrepository

Symfony Doctrine QueryBuilder OneToMany filtering


I have following code in my Repository

// ProductBundle/Repository/ProductRepository.php

$qb->where($qb->expr()->eq('afp.id', 15));
$qb->andWhere($qb->expr()->eq('afp.id', 14));

return $qb
            ->select('a', 'afp')
            ->leftJoin('a.productFields', 'afp')
            ->getQuery()
            ->getResult();

But I always get null return, but I want to get products which have both productFields (so orWhere is not good).


Solution

  • You want to use MEMBER OF instead of comparing id. Otherwise, you're looking for a record that has two different id values, which of course isn't possible.

    This will do what you want:

    $qb->where($qb->expr()->isMemberOf(15, 'a.productFields'));
    $qb->andWhere($qb->expr()->isMemberOf(14, 'a.productFields'));