php - 交响乐团 : Pagination with inner join

标签 php symfony doctrine-orm pagination

我需要实现分页。貌似 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 中拥有指向 DeclarationManyToOne 关系要好得多。这样,它就会起作用,因为您将不再有两个 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/

相关文章:

php - simplexml_load_file() : I/O warning : failed to load external entity &quot;/user-bundle/Resources/config/doctrine/model/User. orm.xml

php - 为什么文件首先上传到临时目录?

php - 如何从 Cross Slide 图像库的数据库中获取图像?

Symfony2 从实体注释翻译断言消息

php - symfony2 的 sfguard 包

php - Doctrine 注册自定义 DQL 函数

Symfony2/Doctrine 使 $statement->execute() 不是 "buffer"所有值

symfony - Doctrine2和FOSElasticaBundle之间的同步不是实时的吗?

php - HTML "name"属性中可以使用非西方字符吗?

php - Android GCM 中的通知生成问题