mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
LEFT JOIN results with optional references are being hydrated AND move to managed state #6617
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 @boesing on GitHub (Jan 29, 2021).
Hey there,
I dont have a current example but last year, I was tuning some of our code to avoid these "PARTIAL LOAD" queries which are executed when an entity is being fetched from the database.
The only thing I remember is, that we had the following case:
As the relation is not necessarily required, I am using
leftJoinin the Query Builder and add the table b entity to theselectclause.When the query was executed, I've received the hydrated entity for table a with an hydrated entity of table b (containing only
nullvalues). As this aint annoying enough, that entity of table b was also considered as managed state in the UnitOfWork.I am not sure if you guys can follow me but I consider this as a bug.
Any ideas on how I could fix that? I'm willing to create a PR regarding this as I think checking if the primary key of entity B not being null could be an indicator of an existing reference, right?
@alexpkalinka commented on GitHub (Feb 3, 2021):
Today I experienced exactly the same bug, but weirdly enough it was caused by a typo in the
select()clause. I put the same table alias twice in theselect()and that table wasleftJoin()-ed. Something like this:If
p.childisNULLthencshouldn't be hydrated. But as @boesing described, the tablecwas hydrated withnulls instead. And in my case it was even more critical since I have type-hinted fields in the classes, so when Doctrine tried to set non-nullable properties asnullPHP crashed with errors likeTyped property Child::$name must be string, null used.However, if you remove the second
cthen the bug will disappear.I still think it's a bug in Doctrine, but it seems like it only happens when you put the same table alias twice in the
select()statement.Doctrine ORM version:
2.8.1@mindaugasvcs commented on GitHub (Nov 21, 2023):
I think this might be useful for somebody.
Actually defining entity relations is required for Doctrine to properly hydrate objects in leftJoin when using entity class instead of relation field.
I don't know if it's a bug or intentional but changing CommentReaction::class to 'c.reactions' and definning the relation fixed my issue: https://github.com/doctrine/orm/issues/5595#issuecomment-1818016632