mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
DDC-3224: getResult(HYDRATE_OBJECT) with joined query is returning reduced number of rows #3992
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Jul 23, 2014).
Originally assigned to: @Ocramius on GitHub.
Jira issue originally created by user gondo:
given that i have these 2 entities (pseodocode):
tables and data
entity1:
|| id || name ||
| 1 | Jhon |
| 2 | Clare |
entity2:
|| id || date || entity1_id ||
| 1 | 2011-01-01 00:00:01 | 1 |
| 2 | 2012-02-02 00:00:02 | 1 |
| 3 | 2013-03-03 00:00:03 | 2 |
| 4 | 2014-04-04 00:00:04 | 2 |
my query builder
proper result is this:
|| id || name || date ||
| 1 | Jhon | 2011-01-01 00:00:01 |
| 1 | Jhon | 2012-02-02 00:00:02 |
| 2 | Clare | 2013-03-03 00:00:03 |
| 2 | Clare | 2014-04-04 00:00:04 |
what is happening
is really returning proper number of rows
BUT and here comes the BUG finally:
is returning just 2 rows:
|| id || name || date ||
| 1 | Jhon | 2011-01-01 00:00:01 |
| 2 | Clare | 2013-03-03 00:00:03 |
this is because somehow entities are made unique.
my workaround
as a workaround, what i have to do is, to exectute 2 queries. 1st to get just Entity1.ids joined with Entity2.dates by using
getArrayResult()and second query to get Entity1 objeces by unique ids from 1st query.
and than manualy join those results in php.
@doctrinebot commented on GitHub (Jul 23, 2014):
Comment created by @ocramius:
I see that you are using
Join::WITH, but not providing a conditional in the example. Are you filtering a fetch-joined association?@doctrinebot commented on GitHub (Jul 23, 2014):
Comment created by gondo:
sorry i've tried to simplify my structure as much as it was possible, there are actually real conditions, one of them is date condition (among many others). i've updated my code
@doctrinebot commented on GitHub (Jul 23, 2014):
Comment created by @ocramius:
There are still some inconsistencies in the issue - where is that query parameter used, for example?
@doctrinebot commented on GitHub (Jul 23, 2014):
Comment created by gondo:
im using it in EntityRepository (sorry, didnt know thats important) i ll update my code
and the whole code is in Symfony2 project (web and command line applications)
@doctrinebot commented on GitHub (Jul 23, 2014):
Comment created by @ocramius:
What I mean is that in
->setParameter('date', new \DateTime('last month')), parameter:datedoes not exist in the DQL.@doctrinebot commented on GitHub (Jul 23, 2014):
Comment created by gondo:
i see, sorry :) its part of JOIN condition, i've updated the code
@TomasPilar commented on GitHub (Jan 6, 2016):
Hi, i just spent the few hours with debugging my app since I encountered the same bug that describes Gondo. With method getArrayResult() is the result OK but with method getResult() the result contains fewer items.
@Ocramius commented on GitHub (Jan 6, 2016):
Re-looked at this: seems like expected behavior from my point of view.
@maresja1 commented on GitHub (Jan 20, 2016):
Seems like an expected behavior to me too.
@Ocramius commented on GitHub (Jan 20, 2016):
Closing. Needs to be reproduced by a test, if re-opened.
@gondo commented on GitHub (Oct 21, 2016):
@Ocramius , @maresja1 can you please explain how is this expected behaviour?
getArrayResult()andgetResult()are returning different number of results. aren't this functions suppose to return only different types of results?@maresja1 commented on GitHub (Nov 28, 2016):
@gondo Because of how hydratation works - @Ocramius will correct me, if I'm wrong - but when you're hydratating objects (
getResult()) you don't expect the same instance to be returned twice, thus when you join related table (let's call it B), having more rows linked to the same instance of parent object (call it A), Doctrine "groups" the repeating values of an instance of A, so you get each instance from A just once.The related instances of class B are hydratated separately and that is why from one query you can get one instance of A having hydrateted collection of multiple instances of B.
This is nicely described (with the connected performance drawbacks) in this article written by @Ocramius:
https://ocramius.github.io/blog/doctrine-orm-optimization-hydration/
@gondo commented on GitHub (Nov 28, 2016):
@maresja1 thanks for taking time to write the response. i will certainly read the linked article.
but this behaviour is counter-intuitive and without studying the
wana be expectedbehaviour, one gets confused easily.@Ocramius commented on GitHub (Dec 4, 2016):
This is indeed expected behavior (unless someone writes a test that shows a problem here), as per fetch join semantics (@maresja1's comment).
An ORM loads an object graph into memory, and there is always only one instance of a combination of entity identifier and entity type ever inside a single
EntityManagerinstance. The de-duplication is applied on purpose.@ybenhssaien commented on GitHub (Feb 5, 2019):
Having the same problem here without Join, just using a simple select with queryBuilder
Code :
getResult() return :
While getArrayResult() return :
Didn't deeply investigate but seems a strange behaviour !
@Ocramius commented on GitHub (Feb 5, 2019):
@youssefbenhssaien that's normal hydration behaviour. See also https://ocramius.github.io/blog/doctrine-orm-optimization-hydration/ for an explanation of what's going on. If you feel that this isn't clear from the official documentation, please also propose a patch there.