![](/img/trans.png)
[英]How to configure entities in zf2 for ManyToOne unidirectional doctrine mapping
[英]How to join two entities with doctrine 2 and zf2?
我有两个实体Carros
和Msg
,我希望获得具有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:\\网站\\自动\\供应商\\ zendframework \\ zendframework \\ library \\ Zend \\ Paginator \\ Paginator.php:637
Mensagem:产生迭代器时出错
C:\\ websites \\ auto \\ vendor \\ doctrine \\ orm \\ lib \\ Doctrine \\ ORM \\ Tools \\ Pagination \\ WhereInWalker.php:85
Mensagem:
无法计数选择两个FROM组件的查询,无法区分
当我尝试这样做时,它会产生错误:
foreach ($paginator as $carro)
{}
得到这样的结果时,错误消失了:
$fi = $query->getResult();
接着
$paginator = new \Zend\Paginator\Paginator(new
\Zend\Paginator\Adapter\ArrayAdapter($fi)
);
如果您具有ManyToOne关系,则可以执行以下操作(确保正确映射之前,请务必先查看此链接: Bidirectionalnal ManyToOne关系
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联接两个表。 因此,我的分页器不会向我显示您的错误。
编辑:使用参数我可以决定是否分页。 如果不需要,请不要使用它。
我不再使用新的DoctrinePaginator(new ORMPaginator($ query))
现在我正在使用\\ Zend \\ Paginator \\ Paginator及其工作$ fi = $ query-> getResult();
$ paginator = new \\ Zend \\ Paginator \\ Paginator(new \\ Zend \\ Paginator \\ Adapter \\ ArrayAdapter($ fi));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.