我有两个实体共享一个抽象类,创建类表继承。我可以使用抽象类的存储库查询实体,并获取扩展抽象类的所有实体作为结果。
$qb = $this->createQueryBuilder('c')
->where('c.featured = true')
->orderBy('c.sticky', 'DESC')
->addOrderBy('c.weight', 'ASC')
->setFirstResult($offset)
->setMaxResults($limit);
// Returns 8 results, results in 34 queries
子类包含与其他实体的多对多关系,因此如果我以这种方式查询,这些关系会导致额外的查询,因为它们没有被连接。如何查询扩展抽象类的实体并加入它们的列?我尝试使用左连接添加多个 from 语句,但查询返回的结果少于预期的 8 个结果。该查询生成器看起来像这样:
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select(array(
'a', 'artist', 'country',
't', 'artwork', 'user'))
->from('AcmeArtworkBundle:Artwork', 'a')
->from('AcmeTourBundle:Tour', 't')
->leftJoin('a.artist', 'artist')
->leftJoin('a.country', 'country')
->leftJoin('t.artwork', 'artwork')
->leftJoin('t.user', 'user')
->where('a.featured = true')
->andWhere('t.featured = true')
->orderBy('a.sticky', 'DESC')
->addOrderBy('t.sticky', 'DESC')
->addOrderBy('a.weight', 'ASC')
->addOrderBy('t.weight', 'ASC')
->setFirstResult($offset)
->setMaxResults($limit);
// 5 results :-(