Symfony2内部联接返回结果数组中的代理

时间:2013-12-06 17:33:26

标签: php symfony orm doctrine-orm

我知道这个查询工作就像一个魅力:

$result = $this->getEntityManager()->createQueryBuilder()
    ->select('banner','customer')
    ->from("MyBundle:Banner", 'banner')
    ->innerJoin('banner.customer', 'customer')
    ->getQuery()
    ->getResult();

稍后,在视图中,我可以迭代并打印{{banner.name}}{{banner.customer.name}},而无需额外查询数据库。

但是,相同的查询对innerJoin进行了一些更改:

$result = $this->getEntityManager()->createQueryBuilder()
    ->select('banner','customer')
    ->from("MyBundle:Banner", 'banner')
    ->innerJoin('MyBundle:Customer', 'customer', 'WITH', 'customer.id = banner.customer')
    ->getQuery()
    ->getResult();

foreach ($result as $e)
    echo get_class($e).'<br/>';
die(1);

此印刷品:

MyBundle\Entity\Banner
Proxies\__CG__\MyBundle\Entity\Customer
MyBundle\Entity\Banner
MyBundle\Entity\Banner
Proxies\__CG__\MyBundle\Entity\Customer
MyBundle\Entity\Banner
MyBundle\Entity\Banner
Proxies\__CG__\MyBundle\Entity\Customer
MyBundle\Entity\Banner

所以,如果我迭代这个结果,期待一个MyBundle\Entity\Banner对象,我将遇到麻烦,所以:

这是第二个例子中innerJoin的预期结果吗? 是否需要额外的function callsyntaxdoctrine/symfony setup才能使其像第一个示例一样工作?

感谢

1 个答案:

答案 0 :(得分:0)

使用双向oneToMany并从WITH电话中删除innerJoin

Customer:
  type: entity
  oneToMany:
    banners:
      targetEntity: Banner
      mappedBy: customer
Banner:
  type: entity
  manyToOne:
    customer:
      targetEntity: Customer
      inversedBy: banners
      joinColumn:
        name: customer_id
        referencedColumnName: id