doctrine2 loads one-to-many associations with fetch mode eager using too many SQL queries(使用过多的 SQL 查询,doctrine2 加载具有 fetch 模式的一对多关联)
问题描述
我正在加载一个包含许多实体的列表.
这些实体与其他实体之间存在一对多关联.
我想在一个 SQL 查询中加载所有这些其他实体(而不是对第一个列表中的每个实体进行一个查询).
I am loading a list of many entities.
These entities have a one-to-many association to other entities.
I want to load all these other entities in one single SQL query (instead of one query for every entity in the first list).
如doctrine2 文档所述:http://www.doctrine-project.org/docs/orm/2.1/en/reference/dql-doctrine-query-language.html#temporously-change-fetch-mode-in-dql 这应该可以通过EAGER"加载实现.
As discribed in the doctrine2 documentation: http://www.doctrine-project.org/docs/orm/2.1/en/reference/dql-doctrine-query-language.html#temporarily-change-fetch-mode-in-dql this should be possible with "EAGER" loading.
但它不像描述的那样工作.
but it does not work as described.
我的代码:
class User{
/**
* @ORMOneToMany(targetEntity="Address", mappedBy="user", indexBy="id", fetch="EAGER")
*/
protected $addresses;
public function __construct(){
$this->addresses = new ArrayCollection();
}
}
class Address{
/**
* @ORMManyToOne(targetEntity="User", inversedBy="addresses")
* @ORMJoinColumns({
* @ORMJoinColumn(name="UserId", referencedColumnName="id")
* })
*/
private $user;
}
class UserRepository{
public function findUsersWithAddresses(){
return $this->getEntityManager()
->createQuery('SELECT u FROM MyBundle:User u ORDER BY u.name ASC')
->setFetchMode('MyBundleEntityUser', 'addresses', DoctrineORMMappingClassMetadata::FETCH_EAGER)
->setMaxResults(10)
->getResult();
}
}
UserRepository::findUsersWithAddresses() 方法执行 11 个 SQL 查询.
The method UserRepository::findUsersWithAddresses() executes 11 SQL Queries.
如何告诉 Doctrine 只使用一个 SQL 查询来加载地址实体?
我正在使用:
- symfony v2.0.9
- 共同准则 2.1.4
- doctrine-dbal 2.1.5
- 教义 2.1.5
推荐答案
当前版本的学说不支持这个.
The current version of doctrine doesn't support this.
在doctrine2 问题跟踪器中有一个功能请求.
There is a feature request about this in the doctrine2 issue tracker.
所以我希望它能尽快实施.
So I hope it will be implemented soon.
这篇关于使用过多的 SQL 查询,doctrine2 加载具有 fetch 模式的一对多关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用过多的 SQL 查询,doctrine2 加载具有 fetch 模式的一对多关联


基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01