[DDC-357] Lazy-loading in OneToOne-bidirectional associations not working for inverse side Created: 21/Feb/10  Updated: 11/Sep/14  Resolved: 21/Feb/10

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.0-ALPHA4
Fix Version/s: None
Security Level: All

Type: Bug Priority: Minor
Reporter: Marcel Walter Assignee: Benjamin Eberlei
Resolution: Won't Fix Votes: 0
Labels: None

XAMPP 1.7.3

Attachments: File getRelation.php    
Issue Links:
is referenced by DDC-3011 [GH-970] [DDC-357] Effective toOne joins Resolved


I am referring to the following situation:

In this example: if I fetch an object of type "Customer" from my database, a second query is executed immediately that fetches the corresponding "Cart" even if I do not access the $cart property of "Customer". Annotating fetch="LAZY" to the $cart property does not make any difference. This is even worse in case of self-referencing objects, e.g. those having at most one parent object and at most one child object. Here, all associations are created by single database queries at once (e.g. fetching the child object, then the child of the child object and so forth).

By contrast, OneToMany associations are lazy-loaded from the inverse side (as expected).

Perhaps I should add, that I am using annotation mappings for my entities (no YAML, no XML).

Comment by Roman S. Borschel [ 21/Feb/10 ]

This is expected behavior. Inverse sides of one-to-one associations can not be lazy, technically. There is no foreign key on the inverse side, hence it is impossible to decide whether to proxy it or not. We must query for the associated object or join it. Note that this only affects inverse sides of single-valued associations, that is, really only the inverse side of bidirectional one-to-one associations.

In the future, you can use fetch="EAGER" to automatically load the associated objects in a join whenever the inverse side object is loaded. That is a planned enhancement.
So when fetch="EAGER" is used on a single-valued association, it is automatically fetch-joined, even when you just do ->find('Object', 1).

Right now, you can use an eager fetch join in DQL to avoid the extra query. Fetch-joins on single-valued associated are usually very cheap, compared to collections.

Note that you need a join anyway, because the foreign key is on the other side, thus it doesnt make much sense to join just for the sake of getting the foreign key. If we join we can as well grab the associated object completely.

The "fetch" mode is a "hint", that means, Doctrine tries to do that when possible. Its not always possible.

If you have a suggestion, feel free to speak up.

Comment by Roman S. Borschel [ 21/Feb/10 ]

If you have an example of a self-referential association that causes extreme ripple-loading of the whole hierarchy, can you please file a new jira issue for that?


Comment by Roman S. Borschel [ 21/Feb/10 ]

Here some other ways to get around the extra queries:

1) $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);

2) $query->getArrayResult() / ->getScalarResult()

Just for the sake of completeness.

Comment by Konstantin [ 13/Mar/12 ]

Why we cannot create proxy without FK?

Comment by Christian S. [ 10/Dec/12 ]

Hi. I have a problem with this solution.

I have an entity "Document" with an one-to-one association to an entity called "File". In the database table of the File entity I store the content of a file. So if I load for example 20 Documents I do not want the associated Files to be loaded, because this leads to memory problems.

I do not understand the explanation

There is no foreign key on the inverse side, hence it is impossible to decide whether to proxy it or not.

In a one-to-many association there is no foreign key on the inverse side, too. Why not always load a proxy like in a one-to-many association?

Plus: There is no documentation about this, neither on http://docs.doctrine-project.org/en/latest/reference/association-mapping.html nor http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html

Comment by Roman S. Borschel [ 10/Dec/12 ]

It is pretty simple, in a one-to-many association where the one-side is the inverse side, it holds a collection. The collection may be empty or not but there can always be a collection so it easy and correct to always put a proxy collection there.

If you have a single-valued side that is the inverse side, how can you decide whether to put a proxy object there or not? Without seeing the foreign key you have no way to distinguish between: There is no associated object (and thus putting a proxy object in place would by simply wrong) or there is one and of which type it is, if inheritance is involved (since putting a proxy of the wrong type in is also wrong).

This is as far as I recall, maybe Benjamin knows more on the current state.

Comment by Christian S. [ 10/Dec/12 ]

Okay, I see the problem. But it would be nice if this behaviour could be documented.

Isn't it possible to always put a special proxy object there and if it get's accessed via lazy loading and Doctrine detect's that there is no associated entity, then the proxy will be replaced by a NULL value?

Comment by Hernan Rajchert [ 26/Feb/13 ]

Hi, I've been faced with this issue some times. I tent to solve this by doing two unidirectional one-to-one. Then I deal with the non existing relationship using a method called getRelation that I defined in a BaseModel.
I have created this method because Doctrine filled up with a Proxy when I fetch the entity without its relationship (then accesing the object would throw an Exception), and filled up with null when the object was eagerly fetched (left join) but no relationship was found.
I think we could add this getRelation in the entitymanager or a trait after php 5.4.

Comment by Martin Štekl [ 10/Nov/13 ]

I think that idea of special proxy object is not so bad. However if you do not want to create special proxy object and build some logic around it then why do you not want to satisfy at least one of two (currently unsupported) cases?
I mean the case in which the object type is used in inheritance. This case is easier to solve in my opinion (similar to @OneToMany) and usually wanted. It is better to support at least one possibility then none.

Comment by Filip Procházka [ 05/Mar/14 ]

I'm proposing a solution to this "Won't Fix" that I disagree with https://github.com/doctrine/doctrine2/pull/970

Comment by Doctrine Bot [ 23/Mar/14 ]

A related Github Pull-Request [GH-970] was closed:

Comment by Misha Bosiy [ 11/Sep/14 ]

My solution:
Two entities with one-to-one reference Site and Url:




  • @var Url
  • @ORM\OneToOne(targetEntity="Url", cascade= {"persist"}


  • @ORM\JoinColumns( { * @ORM\JoinColumn(name="url_id", referencedColumnName="url_id") * }

    private $url;


  • @return Url
    public function getUrl() { return $this->url; }




  • @var \Doctrine\ORM\PersistentCollection
  • @ORM\OneToMany(targetEntity="Site", mappedBy="url")
    private $site;


  • @return Site|null
    public function getSite() { return ($this->site->first() !== false) ? $this->site->first() : null; }



$url = $urlRepo->createQueryBuilder('u')
>where('u.url_id = :url_id')>setParameter('url_id', 19518)

We have only 1 query to DB.

When call:

One more query appears. So lazy load work perfect!


If with join:
$url = $urlRepo->createQueryBuilder('u')
->select('u', 's')
->join('u.site', 's')
>where('u.url_id = :url_id')>setParameter('url_id', 19518)

When call:

Only 1 query to DB.

Comment by Filip Procházka [ 11/Sep/14 ]

That is not a solution but a workaround which we are also using.

Comment by Misha Bosiy [ 11/Sep/14 ]

I wrote it for people who has such problem and don't know what to do with not necessary subqueries.

Generated at Wed Oct 01 00:09:37 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.