我通过递归解决了
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->select('u')
->from('MyBundle:User', 'u');
$qb ->innerJoin('u.visits', 'v');
$qb->andWhere($qb->expr()->in('v.id', $this->getVisitDQL($qb, $visitData, $tagCounter)));
public function getVisitDQL(QueryBuilder $qb, $data, $counter) {
$tag = $data['tags'][$counter];
$tagName = $tag['content'];
$qb2 = $this->getEntityManager()->createQueryBuilder();
$qb2
->select('v'.$counter.'.id')
->from('MyBundle:Visit', 'v'.$counter)
->innerJoin('v'.$counter.'.tags', 'vt'.$counter)
->where($qb2->expr()->eq('vt'.$counter.'.name',':vtag_'.$counter ));
$qb->setParameter(':vtag_'.$counter, $tagName);
// if were not processing the last filter, continue recursion
if ($counter != (count($data['tags'])-1)) {
$qb2->andWhere($qb->expr()->in('v'.$counter.'.id', $this->getVisitDQL($qb, $data, $counter + 1)));
}
return $qb2->getDQL();
}