ObjectHydrator ignores joined scalar values #5551

Closed
opened 2026-01-22 15:11:08 +01:00 by admin · 1 comment
Owner

Originally created by @SirWaddles on GitHub (May 23, 2017).

Originally assigned to: @Ocramius on GitHub.

I'm trying to fetch a subset of data in some collections, and I'm not really sure how to go about it. The way I had planned was to use an aliased value containing the data I wanted to fetch, so that there was no confusion regarding the collection hydration later on (if you fetch the entity, then the joined entity, then it's collection contents, you probably want the whole collection, not a weird subset)

My query looks like

SELECT base as ent, link.name AS subset_names FROM AppBundle\Entity\Document base LEFT JOIN base.users linkown LEFT JOIN linkown.user link WHERE linkown.dateSeen IS NULL

Ideally hoping to select a result with a list entities right next to the filtered names I was after. (A list of users who have not seen the document)

The problem is that subset_names will only ever contain the last name in the result, despite all of them being in the results from the database.

The offending line seems to be in ObjectHydrator.php which replaces the entry every time it sees a new one.

I looked briefly at filtering collections, which could work but would be really messy in the application I'm trying to build.

Is doctrine meant to just ignore data though?

Originally created by @SirWaddles on GitHub (May 23, 2017). Originally assigned to: @Ocramius on GitHub. I'm trying to fetch a subset of data in some collections, and I'm not really sure how to go about it. The way I had planned was to use an aliased value containing the data I wanted to fetch, so that there was no confusion regarding the collection hydration later on (if you fetch the entity, then the joined entity, then it's collection contents, you probably want the whole collection, not a weird subset) My query looks like ``` SELECT base as ent, link.name AS subset_names FROM AppBundle\Entity\Document base LEFT JOIN base.users linkown LEFT JOIN linkown.user link WHERE linkown.dateSeen IS NULL ``` Ideally hoping to select a result with a list entities right next to the filtered names I was after. (A list of users who have not seen the document) The problem is that subset_names will only ever contain the last name in the result, despite all of them being in the results from the database. The offending line seems to be in [ObjectHydrator.php](https://github.com/doctrine/doctrine2/blob/22ecc2d58cdf1e8a9b932d99b952c94afae7e873/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php#L548) which replaces the entry every time it sees a new one. I looked briefly at [filtering collections](http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections), which could work but would be really messy in the application I'm trying to build. Is doctrine meant to just ignore data though?
admin added the Invalid label 2026-01-22 15:11:08 +01:00
admin closed this issue 2026-01-22 15:11:08 +01:00
Author
Owner

@Ocramius commented on GitHub (Jun 1, 2017):

Is doctrine meant to just ignore data though?

Doctrine will de-duplicate based on the root of your selection (base as ent), so this seems like expected behavior. You seem to want just some scalar values, so I suggest fetching the identifier of the base entity, and then you shouldn't have this de-duplication anymore.

@Ocramius commented on GitHub (Jun 1, 2017): > Is doctrine meant to just ignore data though? Doctrine will de-duplicate based on the root of your selection (`base as ent`), so this seems like expected behavior. You seem to want just some scalar values, so I suggest fetching the identifier of the `base` entity, and then you shouldn't have this de-duplication anymore.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5551