doctrine fetch lazy not works on onetoone relationships

For proxying you need: a) the id of the other object and b) know what concrete type the other object is so that you can use the right proxy.
"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.

评论

热门博文