Undefined Property error in ObjectHydrator when using JOIN WITH #5478

Closed
opened 2026-01-22 15:08:47 +01:00 by admin · 2 comments
Owner

Originally created by @SirWaddles on GitHub (Mar 27, 2017).

Originally assigned to: @lcobucci on GitHub.

I have a pretty strange issue that I can't quite work out, I have a fix but it's affecting code from 8yrs ago which seems pretty stable.

I have a DQL query resembling

    SELECT base, fruits, apples, data
    FROM AppBundle\Base base 
    LEFT JOIN base.fruits fruits 
    LEFT JOIN AppBundle\Apple apples WITH fruits.id = apples.id 
    LEFT JOIN apples.data data

where a Base can have many Fruits of multiple types. The error I get when trying to run this query is just
Undefined property on AppBundle\Base::$data

For some reason, the ObjectHydrator is trying to add the values retrieved from the database to the wrong entity. After doing some debugging, I found something a little odd in ObjectHydrator.php

    // Get a reference to the parent object to which the joined element belongs.
    if ($this->_rsm->isMixed && isset($this->rootAliases[$parentAlias])) {
        $first = reset($this->resultPointers);
        $parentObject = $first[key($first)];

It appears that it can only ever get a reference to the first object that there is a root alias to. After changing the second line to $first = $this->resultPointers[$parentAlias]; in a debug build, my problem is solved and although I didn't run through the unit tests (will do if it's a PR, but it seems more like I'm doing something wrong here) my application elsewhere works fine.

I realise there is a limitation when it comes to downcasting in Doctrine (which is what I am trying to do) but it seems like this issue would have broader implications.

Originally created by @SirWaddles on GitHub (Mar 27, 2017). Originally assigned to: @lcobucci on GitHub. I have a pretty strange issue that I can't quite work out, I have a fix but it's affecting code from 8yrs ago which seems pretty stable. I have a DQL query resembling ``` SELECT base, fruits, apples, data FROM AppBundle\Base base LEFT JOIN base.fruits fruits LEFT JOIN AppBundle\Apple apples WITH fruits.id = apples.id LEFT JOIN apples.data data ``` where a Base can have many Fruits of multiple types. The error I get when trying to run this query is just `Undefined property on AppBundle\Base::$data` For some reason, the ObjectHydrator is trying to add the values retrieved from the database to the wrong entity. After doing some debugging, I found something a little odd in [ObjectHydrator.php](https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php#L360) ```php // Get a reference to the parent object to which the joined element belongs. if ($this->_rsm->isMixed && isset($this->rootAliases[$parentAlias])) { $first = reset($this->resultPointers); $parentObject = $first[key($first)]; ``` It appears that it can only ever get a reference to the first object that there is a root alias to. After changing the second line to `$first = $this->resultPointers[$parentAlias];` in a debug build, my problem is solved and although I didn't run through the unit tests (will do if it's a PR, but it seems more like I'm doing something wrong here) my application elsewhere works fine. I realise there is a limitation when it comes to downcasting in Doctrine (which is what I am trying to do) but it seems like this issue would have broader implications.
admin added the Bug label 2026-01-22 15:08:47 +01:00
admin closed this issue 2026-01-22 15:08:47 +01:00
Author
Owner

@lcobucci commented on GitHub (Mar 27, 2017):

This is a known limitation of the ORM when working with class table inheritance and might not be properly documented.

@SirWaddles would be nice if you could reproduce and implement a good (tested) solution for this on a PR.

@lcobucci commented on GitHub (Mar 27, 2017): This is a known limitation of the ORM when working with class table inheritance and might not be properly documented. @SirWaddles would be nice if you could reproduce and implement a good (tested) solution for this on a PR.
Author
Owner

@lcobucci commented on GitHub (May 2, 2017):

Fixed by #6367

@lcobucci commented on GitHub (May 2, 2017): Fixed by #6367
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5478