DDC-1358: Native Query hydration ignores empty entity doublons #1704

Closed
opened 2026-01-22 13:22:43 +01:00 by admin · 10 comments
Owner

Originally created by @doctrinebot on GitHub (Sep 2, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user hypolite:

My case is the follwing :

In a range of dates, I want to know if an entity has been defined for each day. So the raw SQL result would looks like :

DATE              ID NAME
--------- ---------- -------------
19-SEP-11          4 [Entity Name]
20-SEP-11          1 [Entity Name]
21-SEP-11          2 [Entity Name]
22-SEP-11          3 [Entity Name]
23-SEP-11       NULL NULL
24-SEP-11       NULL NULL
25-SEP-11       NULL NULL
26-SEP-11       NULL NULL
27-SEP-11          7 [Entity Name]
28-SEP-11          6 [Entity Name]
29-SEP-11          5 [Entity Name]

11 rows selected.

The range calculation forced me to use Native Query, but I stumbled upon a strange behavior : the Result array showed less records than the raw SQL would provide. After some dirty debugging, I found out that during hydration, an array of previously hydrated object keys was stored ObjectHydrator->_identifierMap. The key of null elements (the gap in the SQL result above) is stored too. The problem is that when there is multiple empty elements, hydration only keeps one, and forget the others, thus reducing the overall size of the output result.

I've already come up with a quick n'dirty fix wich suits my current needs, but it would be better if this issue was addressed at a more global level.

Here's the one-liner fix :

                if ( ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]]) ) {
                if ( ! isset($this->*identifierMap[$dqlAlias][$id[$dqlAlias]]) ](| str*replace( '|', '', $id[$dqlAlias)) == '') {
Originally created by @doctrinebot on GitHub (Sep 2, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user hypolite: My case is the follwing : In a range of dates, I want to know if an entity has been defined for each day. So the raw SQL result would looks like : ``` sql DATE ID NAME --------- ---------- ------------- 19-SEP-11 4 [Entity Name] 20-SEP-11 1 [Entity Name] 21-SEP-11 2 [Entity Name] 22-SEP-11 3 [Entity Name] 23-SEP-11 NULL NULL 24-SEP-11 NULL NULL 25-SEP-11 NULL NULL 26-SEP-11 NULL NULL 27-SEP-11 7 [Entity Name] 28-SEP-11 6 [Entity Name] 29-SEP-11 5 [Entity Name] 11 rows selected. ``` The range calculation forced me to use Native Query, but I stumbled upon a strange behavior : the Result array showed less records than the raw SQL would provide. After some dirty debugging, I found out that during hydration, an array of previously hydrated object keys was stored `ObjectHydrator->_identifierMap`. The key of null elements (the gap in the SQL result above) is stored too. The problem is that when there is multiple empty elements, hydration only keeps one, and forget the others, thus reducing the overall size of the output result. I've already come up with a quick n'dirty fix wich suits my current needs, but it would be better if this issue was addressed at a more global level. Here's the one-liner fix : ``` if ( ! isset($this->_identifierMap[$dqlAlias][$id[$dqlAlias]]) ) { ``` ``` if ( ! isset($this->*identifierMap[$dqlAlias][$id[$dqlAlias]]) ](| str*replace( '|', '', $id[$dqlAlias)) == '') { ```
admin added the Bug label 2026-01-22 13:22:43 +01:00
admin closed this issue 2026-01-22 13:22:45 +01:00
Author
Owner

@doctrinebot commented on GitHub (Sep 4, 2011):

Comment created by @beberlei:

Doesnt he still hydrate an empty object for this row then? Why are you even using the object hydrator? I rather tend to go and throw an exception if for a query using the ObjectHydrator $id[$dqlAlias] is empty, because frankly this is just not supported.

@doctrinebot commented on GitHub (Sep 4, 2011): Comment created by @beberlei: Doesnt he still hydrate an empty object for this row then? Why are you even using the object hydrator? I rather tend to go and throw an exception if for a query using the ObjectHydrator $id[$dqlAlias] is empty, because frankly this is just not supported.
Author
Owner

@doctrinebot commented on GitHub (Sep 4, 2011):

Comment created by hypolite:

He does hydrate one empty object for the first line with empty ID, but he does not hydrate further empty object.

Thus, instead of having an array with the following objects :

Entity(id=4)
Entity(id=1)
Entity(id=2)
Entity(id=3)
Entity(id=)
Entity(id=)
Entity(id=)
Entity(id=)
Entity(id=7)
Entity(id=6)
Entity(id=5)

I got :

Entity(id=4)
Entity(id=1)
Entity(id=2)
Entity(id=3)
Entity(id=)
Entity(id=7)
Entity(id=6)
Entity(id=5)
@doctrinebot commented on GitHub (Sep 4, 2011): Comment created by hypolite: He does hydrate one empty object for the first line with empty ID, but he does not hydrate further empty object. Thus, instead of having an array with the following objects : ``` Entity(id=4) Entity(id=1) Entity(id=2) Entity(id=3) Entity(id=) Entity(id=) Entity(id=) Entity(id=) Entity(id=7) Entity(id=6) Entity(id=5) ``` I got : ``` Entity(id=4) Entity(id=1) Entity(id=2) Entity(id=3) Entity(id=) Entity(id=7) Entity(id=6) Entity(id=5) ```
Author
Owner

@doctrinebot commented on GitHub (Sep 4, 2011):

Comment created by @beberlei:

Yes, but can you show me your DQL statement?

@doctrinebot commented on GitHub (Sep 4, 2011): Comment created by @beberlei: Yes, but can you show me your DQL statement?
Author
Owner

@doctrinebot commented on GitHub (Sep 5, 2011):

Comment created by hypolite:

I'm using Native Query, so I guess it's not DQL, but here's the query (Oracle SQL)

SELECT *
FROM (
    SELECT to*date(:date_start,\'dd-mm-yyyy\') <ins> rownum -1 as period*date
    FROM all_objects
    WHERE rownum <= to*date(:date_end,\'dd-mm-yyyy\') - to_date(:date*start,\'dd-mm-yyyy\')</ins>1
)
LEFT JOIN entity ON period*date = entity*date',
@doctrinebot commented on GitHub (Sep 5, 2011): Comment created by hypolite: I'm using Native Query, so I guess it's not DQL, but here's the query (Oracle SQL) ``` SELECT * FROM ( SELECT to*date(:date_start,\'dd-mm-yyyy\') <ins> rownum -1 as period*date FROM all_objects WHERE rownum <= to*date(:date_end,\'dd-mm-yyyy\') - to_date(:date*start,\'dd-mm-yyyy\')</ins>1 ) LEFT JOIN entity ON period*date = entity*date', ```
Author
Owner

@doctrinebot commented on GitHub (Oct 15, 2011):

Comment created by @beberlei:

For the Array- and ObjectHydrator the ID is a required result.

I am not sure how to proceed with this, i think the right behavior is to return null as the object when no id is found. What do you think?

Is the Date a scalar value of the result?

@doctrinebot commented on GitHub (Oct 15, 2011): Comment created by @beberlei: For the Array- and ObjectHydrator the ID is a required result. I am not sure how to proceed with this, i think the right behavior is to return null as the object when no id is found. What do you think? Is the Date a scalar value of the result?
Author
Owner

@doctrinebot commented on GitHub (Oct 15, 2011):

Comment created by hypolite:

The expected results would be, in order of preference :

  • Entity without id set (like in my example above)
  • null value

And yes, the Date was an additional scalar value for which I needed the Native Query :)

@doctrinebot commented on GitHub (Oct 15, 2011): Comment created by hypolite: The expected results would be, in order of preference : - Entity without id set (like in my example above) - null value And yes, the Date was an additional scalar value for which I needed the Native Query :)
Author
Owner

@doctrinebot commented on GitHub (Oct 16, 2011):

Comment created by @beberlei:

Your example lacks details but, for case one. Did the queries return values for the non-id columns of this entity?

@doctrinebot commented on GitHub (Oct 16, 2011): Comment created by @beberlei: Your example lacks details but, for case one. Did the queries return values for the non-id columns of this entity?
Author
Owner

@doctrinebot commented on GitHub (Oct 16, 2011):

Comment created by hypolite:

Sorry for the concision, I didn't wanted to flood my comment with unrelated data.

The fact is the queries returned values for the non-id columns when the ID was set, and null values when not. I will update my comment.

@doctrinebot commented on GitHub (Oct 16, 2011): Comment created by hypolite: Sorry for the concision, I didn't wanted to flood my comment with unrelated data. The fact is the queries returned values for the non-id columns when the ID was set, and null values when not. I will update my comment.
Author
Owner

@doctrinebot commented on GitHub (Oct 16, 2011):

Comment created by @beberlei:

I will go with adding NULL values there. This is more consistent, since null represents a non-existent entity.

Fixed and merged back into 2.1.x

@doctrinebot commented on GitHub (Oct 16, 2011): Comment created by @beberlei: I will go with adding NULL values there. This is more consistent, since null represents a non-existent entity. Fixed and merged back into 2.1.x
Author
Owner

@doctrinebot commented on GitHub (Oct 16, 2011):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Oct 16, 2011): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1704