Doctrine2查询与来自不同Symfony2束的多个实体的选择 [英] Doctrine2 query with select on multiple entities from different Symfony2 bundles
问题描述
关系是:
Promo - PromoPeriod:多对一。
PromoPeriod - Shop:一对多。
在实体Promo的存储库中,我尝试获取所有的促销和商店如下:
return $ this-> getEntityManager()
- > createQuery select p,s
FROM AcmePromoBundle:Promo p JOIN p.period pp JOIN pp.shops s)
- > getResult();
其中:p.period是Promo实例p和PromoPeriod周期之间的关系; pp.shops是PromoPeriod pp和商店之间的关系。
返回以下错误:
渲染模板期间抛出异常
( 没有找到具有别名's'的实体结果的父对象
父别名是'pp'。)
所以,我认为原则不能理解s(这是一个商店实体)的类型,因为它位于另一个包中。因此,我试图添加一个INSTANCE OF子句:
SELECT p,s
FROM AcmePromoBundle:Promo p JOIN p。期间pp JOIN pp.shops s
在哪里可以看到AcmeShopBundle:Shop
再一次没有。也许这不是解决问题的方法。
任何想法?
您应该选择3个实体,如下所示
SELECT p,pp,s
FROM AcmePromoBundle:促销p
JOIN p.period pp
JOIN pp.shops s
你'做一个提取连接,即:doctrine将返回查询根目录(这里是Promo(s))的实体,并将其与其他select实体进行合并。
所以,如果你告诉学说来加载商店,那么从开始,而不是选择这个时期,他不能做你要求做的事情。
这里,SQL查询正常,这是当doctrine尝试水合引发异常的对象时。
阅读相关文档 http://docs.doctrine-project.org/en/latest/reference/dql- doctrine-query-language.html#join
Hello I have three Doctrine2 entities in my Symfony2 application: two ("Promo" and "PromoPeriod") in the bundle Acme:PromoBundle; one ("Shop") in the bundle Acme:ShopBundle. The relationships are: Promo - PromoPeriod: Many-to-One. PromoPeriod - Shop: One-to-Many.
In the repository of the entity "Promo", I try to get all the promo and shops as follows:
return $this->getEntityManager()
->createQuery("SELECT p, s
FROM AcmePromoBundle:Promo p JOIN p.period pp JOIN pp.shops s")
->getResult();
where: p.period is the relationship between the Promo instance p and the PromoPeriod period; pp.shops is the relationship between the PromoPeriod pp and the Shops. The following error is returned:
An exception has been thrown during the rendering of a template
("The parent object of entity result with alias 's' was not found.
The parent alias is 'pp'.")
So, I argued that Doctrine is not capable of understanding the type of "s" (which is a Shop entity), since it is located in another bundle. Hence I tried to add an INSTANCE OF clause:
SELECT p, s
FROM AcmePromoBundle:Promo p JOIN p.period pp JOIN pp.shops s
WHERE s INSTANCE OF AcmeShopBundle:Shop
And again nothing. Perhaps this is not the way to solve the problem.
Any idea?
You should select the 3 entities, like so
SELECT p, pp, s
FROM AcmePromoBundle:Promo p
JOIN p.period pp
JOIN pp.shops s
You're doing a fetch join, ie: doctrine will return the entities of the root of the query (here Promo(s)), and hydrate it with the additional select entities. So if you tell doctrine to load the shops, that "come" from period, but to not select the period ... he can't do what you're asking it to do. Here, the SQL query goes fine, it's when doctrine tries to hydrate objects that it throws an exception.
Read the related documentation http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#joins
这篇关于Doctrine2查询与来自不同Symfony2束的多个实体的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!