【问题标题】:Symfony : Pagination with inner joinSymfony:内部连接的分页
【发布时间】:2014-08-01 15:28:58
【问题描述】:

我需要实现分页。貌似,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"

如何在不使用其他捆绑软件或其他任何东西的情况下规避此问题。

希望你能理解我。

【问题讨论】:

    标签: php symfony doctrine-orm pagination


    【解决方案1】:

    您的Declaration 是否与Contact 有某种关联?

    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();
    

    【讨论】:

    • 是声明与联系人相关:@ORM\ManyToOne(targetEntity="Sga\ContactBundle\Entity\Contact", inversedBy="declarations", cascade={"persist", "merge"} ) * @ORM\JoinColumns({ * @ORM\JoinColumn(name="contact_id", referencedColumnName="id") * })
    • 优秀。然后只需将innerJoin 行替换为我写的行即可。
    • 我已经用你的替换了我的,但它不起作用。它不识别 c.不知道哪个类与c相关是正常的。
    • $query->select('d') ->from('SgaDemandeBundle:Declaration', 'd') ->innerJoin('d.contact = c') ->where('c .structure_id = :structure_id') ->setParameter('structure_id', $structureId) ->orderBy('d.id', 'ASC') ->getQuery() ->getResult();我得到“[Syntax Error] line 0, col 68: Error: Expected end of string, got 'c'”
    • 不是innerJoin('d.contact = c'),而是innerJoin('d.contact', 'c')。我已经编辑了答案以包含完整的QueryBuilder 构造...
    【解决方案2】:

    终于找到了解决办法:

    代替:

    $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();
    

    我用过:

    $query->select('d')
          ->add('from', 'SgaDemandeBundle:Declaration d INNER JOIN d.contact c')
          ->where('c.structure_id = :structure_id')
          ->setParameter('structure_id', $structureId)
          ->orderBy('d.id', 'ASC')
          ->getQuery();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      • 2012-09-01
      • 2021-06-24
      • 2017-03-19
      • 2019-12-08
      相关资源
      最近更新 更多