我需要实现分页。貌似 Doctrine 不支持某些关节。
这是我的查询:
$query = $this->getEntityManager()
->createQueryBuilder();
$query->setFirstResult(($page - 1) * $maxperpage);
$query->setMaxResults($maxperpage);
$query->select('d')
->from('DemandeBundle:Declaration', 'd')
->orderBy('d.id', 'ASC')
->innerJoin('ContactBundle:Contact', 'c', 'WITH', 'd.contact = c')
->where('c.structure_id = :structure_id')
->setParameter('structure_id', $structureId)
->getQuery()
->getResult();
return new Paginator($query, true);
当我不使用innerJoin时它工作正常,但我需要使用它以便仅显示与我的用户有关的请求。
使用innerJoin我得到了那种错误:
"An exception has been thrown during the rendering of a template
("Cannot count query which selects two FROM components, cannot make distinction") in
DemandeBundle:Demande:listing_demande.html.twig at line 25"
如何在不使用其他 bundle 或其他方式的情况下规避此问题。
希望你能理解我。
最佳答案
您的声明
与联系方式
有某种关系吗?
在 Contact
中拥有指向 Declaration
的 ManyToOne
关系要好得多。这样,它就会起作用,因为您将不再有两个 FROM 组件,而是只有一个。
然后,修改查询以执行以下操作:
->innerJoin('d.contant', 'c')
完整的查询应如下所示:
$query->select('d')
->from('DemandeBundle:Declaration', 'd')
->orderBy('d.id', 'ASC')
->innerJoin('d.contact', 'c') // <-- THIS LINE IS CRITICAL
->where('c.structure_id = :structure_id')
->setParameter('structure_id', $structureId)
->getQuery()
->getResult();
关于php - 交响乐团 : Pagination with inner join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24165202/