如何使用Doctrine2中的EntityManager检索具有所有关联的实体? [英] How to retrieve an entity with all of its associations using EntityManager in Doctrine2?

查看:70
本文介绍了如何使用Doctrine2中的EntityManager检索具有所有关联的实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的实体,具有多对多和一对多关联。我知道联接用于获取相关的关联,这是针对我的问题的手动解决方案。

I have a simple entity with many-to-many and one-to-many associations. I'm aware of 'Joins' for fetching related associations which is a manual solution for my problem.

如何在EntityManager中使用实体来获取具有其所有关联的实体教义2?例如:

How can I fetch an entity with all of its associations using EntityManager in Doctrine2? e.g.:

$this->em
     ->getRepository('Entities\Patientprofile')
     ->findOneByuserid('555555557')
     ->fetchAllAssociations();


推荐答案

Doctrine 2使用代理类进行延迟加载,因此您在使用对象之前,实际上不需要获取关联的数据。由于Proxy类是从您的关联类继承而来的,因此您可以像使用fretch关联类一样完全使用代理。

Doctrine 2 uses Proxy classes for lazy loading, so you don't actually need to have the associations' data fetched until you use the objects. Since the Proxy classes inherit from your association classes, you're able to use the proxies exactly as you would use the fretch association classes.

但是,如果您确实需要提取实际的关联类,您需要告诉查询将提取模式设置为Doctrine\ORM\Mapping\ClassMetadata :: FETCH_EAGER。如果您使用的是注释,则可以通过以下方式实现:

but, if you really need to fetch the actual association classes, you need to tell the query to set the fetch mode to Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER. If you're using the annotations, you can achieve this with:

例如

/**
 * @ManyToMany(targetEntity="Item", fetch="EAGER")
 */
private $items;

这篇关于如何使用Doctrine2中的EntityManager检索具有所有关联的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆