我知道这个查询工作很有魅力:
$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);
此打印:
MyBundleEntityBanner
Proxies__CG__MyBundleEntityCustomer
MyBundleEntityBanner
MyBundleEntityBanner
Proxies__CG__MyBundleEntityCustomer
MyBundleEntityBanner
MyBundleEntityBanner
Proxies__CG__MyBundleEntityCustomer
MyBundleEntityBanner
所以,如果我迭代这个结果,期望一个MyBundleEntityBanner
对象,我会遇到麻烦,所以:
这是像第二个例子一样进行innerJoin
的预期结果吗
是否需要额外的function call
、syntax
或doctrine/symfony setup
才能使其像第一个示例一样工作?
感谢
使用双向oneToMany
并从innerJoin
调用中删除WITH
:
Customer:
type: entity
oneToMany:
banners:
targetEntity: Banner
mappedBy: customer
Banner:
type: entity
manyToOne:
customer:
targetEntity: Customer
inversedBy: banners
joinColumn:
name: customer_id
referencedColumnName: id