我有2个表用户和组;每个对象组都有一个数组成员[],指向User的某些实例。
我需要使用QueryBuilder构建相同的查询:
SELECT (users instances) FROM User
WHERE (users instances) IN (SELECT Group.members from Group WHERE group.id = $someId)
我怎样才能做到这一点?
答案 0 :(得分:2)
你必须制作2个查询构建器:
$qb2 = $this->em->createQueryBuilder('group')
->select('group.members')
->where('group.id = $someId');
$qb = $this->em->createQueryBuilder('user')
->select(user instances)
-where($qb->expr()->in('user instances', $qb2->getDQL());
它会告诉你它是如何工作的。当然,您必须将此代码调整为您的。
答案 1 :(得分:1)
改善@Eimsas答案可能是:
$em = $this->getEntityManager();
$qb2 = $em->createQueryBuilder('group')
->select('group.members')
->where('group.id = $someId');
$qb = $em->createQueryBuilder('user');
$query = $qb->select(user instances)
->where($qb->expr()->in('user instances', $qb2->getDQL());
$result = $query->getQuery->getResult();