How to retrieve an entity with all of its associations using EntityManager in Doctrine2?(如何使用 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.
如何使用 Doctrine2 中的 EntityManager 获取具有所有关联的实体?例如:
How can I fetch an entity with all of its associations using EntityManager in Doctrine2? e.g.:
$this->em
->getRepository('EntitiesPatientprofile')
->findOneByuserid('555555557')
->fetchAllAssociations();
推荐答案
Doctrine 2 使用代理类进行延迟加载,因此您实际上不需要在使用对象之前获取关联的数据.由于 Proxy 类继承自您的关联类,因此您可以像使用 freetch 关联类一样使用代理.
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.
但是,如果你真的需要获取实际的关联类,你需要告诉查询将获取模式设置为 DoctrineORMMappingClassMetadata::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 DoctrineORMMappingClassMetadata::FETCH_EAGER. If you're using the annotations, you can achieve this with:
例如
/**
* @ManyToMany(targetEntity="Item", fetch="EAGER")
*/
private $items;
这篇关于如何使用 Doctrine2 中的 EntityManager 检索具有所有关联的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Doctrine2 中的 EntityManager 检索具有所有关联的实体?
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01