Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDC-357: Lazy-loading in OneToOne-bidirectional associations not working for inverse side #4389

Closed
doctrinebot opened this issue Feb 21, 2010 · 22 comments
Assignees
Labels

Comments

@doctrinebot
Copy link

Jira issue originally created by user m.walter:

I am referring to the following situation:
http://www.doctrine-project.org/documentation/manual/2_0/en/association-mapping#one-to-one,-bidirectional

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).

@doctrinebot
Copy link
Author

@doctrinebot
Copy link
Author

Comment created by romanb:

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.

@doctrinebot
Copy link
Author

Issue was closed with resolution "Won't Fix"

@doctrinebot
Copy link
Author

Comment created by romanb:

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?

Thanks!

@doctrinebot
Copy link
Author

Comment created by romanb:

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.

@doctrinebot
Copy link
Author

Comment created by koc:

Why we cannot create proxy without FK?

@doctrinebot
Copy link
Author

Comment created by naitsirch:

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 {quote}There is no foreign key on the inverse side, hence it is impossible to decide whether to proxy it or not.{quote}
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

@doctrinebot
Copy link
Author

Comment created by romanb:

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.

@doctrinebot
Copy link
Author

Comment created by naitsirch:

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?

@doctrinebot
Copy link
Author

Comment created by hrajchert:

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.

@doctrinebot
Copy link
Author

Comment created by stekycz:

Hi,
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.

@doctrinebot
Copy link
Author

Comment created by hosiplan:

I'm proposing a solution to this "Won't Fix" that I disagree with #970

@doctrinebot
Copy link
Author

Comment created by @doctrinebot:

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

@doctrinebot
Copy link
Author

Comment created by sNICkerssss:

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


Site

/****

  • @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;
    }

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;
    }

Queries:

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

We have only 1 query to DB.

When call:
$url->getSite();

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)
->getQuery()
->getOneOrNullResult();

When call:
$url->getSite();

Only 1 query to DB.

@doctrinebot
Copy link
Author

Comment created by hosiplan:

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

@doctrinebot
Copy link
Author

Comment created by sNICkerssss:

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

@doctrinebot
Copy link
Author

Comment created by David Kaštánek:

Elegant. I'm also going to use this workaround to reduce the number of unnecessary subqueries. Thanks for sharing!

@ABM-Dan
Copy link

ABM-Dan commented Feb 29, 2016

There are several questions on stackoverload dealing with this, and people are switching to faux ManyToOne relationships and using setters and getters to get the index 0 of the faux array.

Is this really something that we can't possibily implement? I am personally in a situation where I would benefit from such a feature.

@DHager
Copy link
Contributor

DHager commented Feb 29, 2016

Just looking at the dates, is this still an issue? It sounds like folks are looking for $foo->getBar() to return an association proxy, when the table containing Bar data is the one that has a column referring to a Foo's primary key.

If so, can we get a test-case? I'm not sure if #1241 could be relevant, moving forward.

@ABM-Dan
Copy link

ABM-Dan commented Mar 3, 2016

I'm working on crafting a test-case, but I don't really know how to create one as I always use doctrine in Symfony, not sure how to manage it in an independent project. Tips?

@Isinlor
Copy link
Contributor

Isinlor commented Mar 8, 2017

It seems like it may be related to: #970

Solution seems to be outlined in: #970 (comment) (check also previous commit to the linked one)

If it can not be solved, then IMHO it should be clearly documented as a current limitation.

I will try to document it first and then see if it's feasible for me to learn doctrine internals enough to solve it. I don't guarantee anything tough.

nickygerritsen added a commit to DOMjudge/domjudge that referenced this issue Oct 9, 2019
…elation to make lazy loading work.

Doctrine will always load a OneToOne relation where the foreign key is in the *other* table, since that is the only way for it to know whether the value should be NULL or an actual object (or proxy object).
Since the "other side" contains a lot of data that we do not want to load, we turn it into a OneToMany/ManyToOne relation and just get the first value from the collection.
Our code makes sure there is always only one item in the collection.

This idea is based on doctrine/orm#4389 (comment)
@oojacoboo
Copy link
Contributor

oojacoboo commented Apr 20, 2024

So, would it not be possible, since PHP 7.4 now, to check if the property has been initialized? Can that not be sufficient as a "proxy" check for hydration? Obviously that wouldn't be available for nullable fields, but would at least get support for critical non-nullable relationships.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants
@beberlei @DHager @Isinlor @oojacoboo @doctrinebot @ABM-Dan and others