除非我误读你的问题,否则这看起来很简单:
获取具有特定类别的所有帖子:
$qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('p') ->from('SomeBundle:Post', 'p') ->join('p.categories', 'c') ->where('c.id = :categoryId') ->setParameter('categoryId', $categoryId) ->getQuery() ->getResult();
获取包含各种类别的所有帖子:
$qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('p') ->from('SomeBundle:Post', 'p') ->join('p.categories', 'c') ->where($qb->expr()->in('c.id', ':categoryIds')) ->setParameter('categoryIds', $categoryIds) // array of ids ->getQuery() ->getResult();