【发布时间】:2013-12-06 17:33:26
【问题描述】:
我知道这个查询就像一个魅力:
$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 才能使其像第一个示例一样工作?
谢谢
【问题讨论】:
-
你确实有
OneToOne关系集,对吧?如果是这样,那WITH完全没必要…… -
不,是
oneToMany,一个客户有很多横幅 -
哦,那你是把
oneToMany做成双向的吗? -
你能告诉我你的映射吗?由于
oneToMany中的第一个小写字母,我假设您使用YAML来描述关系。检查我的答案 - 我认为你们的关系应该是这样的...... -
是的,如果这就是你的意思,每个人都声明了关系
标签: php symfony orm doctrine-orm