gpt4 book ai didi

doctrine-orm - Doctrine 一对一关系自动加载查询

转载 作者:行者123 更新时间:2023-12-03 08:27:55 25 4
gpt4 key购买 nike

我有一个看起来像这样的查询:

我的用户实体具有如下所示的一对一关系:

/**
* @var UserProfile
*
* @ORM\OneToOne(targetEntity="UserProfile",mappedBy="user")
*/
private $userProfile;

每当我进行查询以选择多个用户对象时,它都会为每个用户创建一个额外的 select 语句来查询 UserProfile 数据,即使我没有通过 get 方法访问它。我并不总是需要 UserProfile 数据,而且我当然不想每次显示用户列表时都加载这些数据。

知道为什么在运行时执行这些查询吗?

最佳答案

这是解决方案的详细解释:

https://groups.google.com/forum/#!topic/doctrine-user/fkIaKxifDqc

"fetch" in the mapping is a hint, that is, if it is possible Doctrine does that, but if its not possible, obviously it does not. Proxying for lazy-loading is simply not always possible, technically. The situations where its not possible are:

1) one-to-one from inverse to owning side (appears only in bidirectional one-to-one associations). Precondition a) above can not be met. 2) one-to-one/many-to-one association to a hierarchy and the targeted class has subclasses (is not a leaf in the class hierarchy). Precondition b) above can not be met.

In these cases, proxying is technically not possible.

Your options to avoid this n+1 problem:

1) fetch-join via DQL: "select c,ca from Customer join c.cart ca". Single query but join, however, joins on to-one associations are relatively cheap.

2) force partial objects. No additional queries but also no lazy-load: $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true)

3) if an alternative result format (i.e. getArrayResult()) is sufficient for a use-case, these also avoid this problem.

Benjamin had some ideas about automatic batching of these loads to avoid n+1 queries but this does not change the fact that proxying is not always possible.

关于doctrine-orm - Doctrine 一对一关系自动加载查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12362901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com