【问题标题】:How to join two entities with doctrine 2 and zf2?如何将两个实体与学说 2 和 zf2 连接起来?
【发布时间】:2015-06-27 18:03:45
【问题描述】:

我有两个实体,CarrosMsg,我正在寻找具有 Msg 消息的 Carros

$query = $entityManager->createQuery("
    SELECT u 
    FROM Auto\Entity\Carros u
    JOIN Auto\Entity\Msg m WITH m.idautoad=u.idcarros
    WHERE u.identidade='".$emailidentidade."'
    ORDER BY u.datadoanuncio DESC
    ");

我正在使用分页器:

    // Create the paginator itself
$paginator = new Paginator(
        new DoctrinePaginator(new ORMPaginator($query))
);

我收到以下错误,我有 zend 2.3.9 和教义 2.4

Arquivo: C:\websites\auto\vendor\zendframework\zendframework\library\Zend\Paginator\Paginator.php:637

Mensagem:生成迭代器时出错

C:\websites\auto\vendor\doctrine\orm\lib\Doctrine\ORM\Tools\Pagination\WhereInWalker.php:85

消息:

无法统计选择两个FROM组件的查询,无法区分

当我尝试这样做时它会产生错误:

foreach ($paginator as $carro)
{}

当得到这样的结果时,错误消失了:

$fi = $query->getResult();

然后

  $paginator = new \Zend\Paginator\Paginator(new
                \Zend\Paginator\Adapter\ArrayAdapter($fi)
          );

【问题讨论】:

标签: php doctrine-orm pagination zend-framework2 zend-paginator


【解决方案1】:

如果您有一个多对一关系,您可以这样做(请务必先查看此链接以使您的映射正确:Bidirectionnal ManyToOne relation

 public function getInvoices($idProformaGroup, $paginate = false)
    {
        $query = $this->getEntityManager()->createQueryBuilder();
        $query->select('po', 'i')
            ->from('Invoice\Entity\Proforma', 'po')
            ->innerJoin('po.idInvoice', 'i')
            ->andWhere("po.idProformaGroup = :idProformaGroup")
            ->orderBy('po.idProforma', 'ASC')
            ->setParameter('idProformaGroup', $idProformaGroup);
        if ($paginate) {
            $doctrineAdapter = new DoctrineAdapter(new ORMPaginator($query, true));
            return new Paginator($doctrineAdapter);
        } else {
            return $query->getQuery()->getResult();
        }
    }

如您所见,我的联接使用外键 po.idInvoice 来联接两个表。因此,我的 Paginator 不会向我显示您的错误。

编辑:使用参数我可以决定是否分页。不需要就不要使用。

编辑 2:从Doctrine : Pagination with left Joins 之前的另一个问题的链接加入 Futurereal 的答案与我试图向您解释的观点相同。

【讨论】:

    【解决方案2】:

    我不再使用了 新的 DoctrinePaginator(新的 ORMPaginator($query))

    现在我正在使用 \Zend\Paginator\Paginator 及其工作 $fi = $query->getResult();

    $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($fi) );

    【讨论】:

      猜你喜欢
      • 2013-12-15
      • 2014-12-21
      • 2013-02-04
      • 2011-07-19
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多