Doctrine orm 原则DQL返回多种类型的实体
我有三个实体:HandsetSubscription、Handset和Subscription 手机订阅的yaml是:Doctrine orm 原则DQL返回多种类型的实体,doctrine-orm,dql,Doctrine Orm,Dql,我有三个实体:HandsetSubscription、Handset和Subscription 手机订阅的yaml是: App\SoBundle\Entity\HandsetSubscription: type: entity table: handset_subscription manyToOne: handset: targetEntity: Handset subscription: targetEntity: Subscription
App\SoBundle\Entity\HandsetSubscription:
type: entity
table: handset_subscription
manyToOne:
handset:
targetEntity: Handset
subscription:
targetEntity: Subscription
id:
id:
type: integer
generator: { strategy: AUTO }
options: { unsigned: true }
fields:
amount:
type: integer
nullable: false
options: { default: 0, unsigned: true }
discount:
type: integer
nullable: false
options: { default: 0, unsigned: true }
查询:
SELECT hs,s,h
FROM \App\SoBundle\Entity\HandsetSubscription hs
JOIN \App\SoBundle\Entity\Subscription s with s.id = hs.subscription
AND s.mins = 150
AND s.mb = 250
AND s.sms = 150
JOIN \App\SoBundle\Entity\Handset h with h.id = hs.handset
以下是检索到的条目的类名:
App\SoBundle\Entity\HandsetSubscription
Proxies\__CG__\App\SoBundle\Entity\Subscription
Proxies\__CG__\App\SoBundle\Entity\Handset
App\SoBundle\Entity\HandsetSubscription
Proxies\__CG__\App\SoBundle\Entity\Handset
App\SoBundle\Entity\HandsetSubscription
Proxies\__CG__\App\SoBundle\Entity\Handset
…
我只希望得到手机订阅实体。为什么我也会得到订阅和手机的代理
通过向手持设备和订阅映射添加fetch-eager,并从查询中的SELECT语句中删除手持设备和订阅,我将只获得手持设备订阅,但我希望通过fetch-join实现这一点,如手册中所述
更新
引用上面发布的链接:
获取地址的联接:
<?php
$query = $em->createQuery("SELECT u, a FROM User u JOIN u.address a WHERE a.city = 'Berlin'");
$users = $query->getResult();
当使用fetch join查询时,它将返回结果数组根级别上FROM子句中的类。在前面的示例中,将返回一个用户实例数组,并将每个用户的地址提取到Useraddress变量中。如果您访问地址,则条令不需要延迟加载与另一个查询的关联。非常感谢条令irc频道的veonik解决了这个问题 您不应使用实体的完全限定名称加入,而应加入关联。因此,查询变成:
SELECT hs,s,h
FROM \App\SoBundle\Entity\HandsetSubscription hs
JOIN hs.subscription s with s.id = hs.subscription
AND s.mins = 150
AND s.mb = 250
AND s.sms = 150
JOIN hs.handset h with h.id = hs.handset
不要选择hs,s,h,只选择hs。订阅和手持设备将通过引用getter和setter提供这是s和h延迟加载的方式。我想让他们迫不及待地上膛。我很喜欢舒尔,但他们不会。不选择,但保留连接。试试看!我又试了一次,结果发现它们都是懒洋洋的。如果您在映射中指定这一点,它们将被加载。但我希望能够从手册中所述的DQL中控制这一点。我添加了一段关于获取连接的理论手册中的引用。正如您所看到的,地址是在SELECT.Works中指定的。你能分享那个DQL的QueryBuilder版本吗?谢谢