且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Doctrine2中的EntityManager检索具有所有关联的实体?

更新时间:2022-01-23 04:28:21

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;