Skip to content

Loading…

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

Closed
doctrinebot opened this Issue · 10 comments

2 participants

@doctrinebot

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)) == '') {
@doctrinebot

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

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

Comment created by @beberlei:

Yes, but can you show me your DQL statement?

@doctrinebot

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

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

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

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

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

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

Issue was closed with resolution "Fixed"

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.1.3 milestone
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.