我知道这个查询工作就像一个魅力:
$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 call
,syntax
或doctrine/symfony setup
才能使其像第一个示例一样工作?
感谢
答案 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