Symfony:内部连接的分页

sca*_*amp 2 php pagination symfony doctrine-orm

我需要实现分页.看来,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);
Run Code Online (Sandbox Code Playgroud)

当我不使用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"
Run Code Online (Sandbox Code Playgroud)

如何在不使用其他捆绑或其他任何东西的情况下绕过这个问题.

希望你能理解我的家伙.

Jov*_*vic 5

你的Declaration某种程度上与之相关Contact吗?

这是迄今为止最好的你有ManyToOne关系的Contact指向Declaration.这样,它将起作用,因为你不会有两个FROM组件,而是单个组件.

然后,修改查询以执行以下操作:

->innerJoin('d.contant', 'c')
Run Code Online (Sandbox Code Playgroud)

完整查询应如下所示:

$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();
Run Code Online (Sandbox Code Playgroud)