Skip to content

Loading…

DDC-568: Notice 'Undefined index' in UnitOfWork when hydrating partial objects with OneToOne assoc (owning side) #5074

Closed
doctrinebot opened this Issue · 6 comments

2 participants

@doctrinebot

Jira issue originally created by user 7heaven:

There's an object with OneToOne association (the owning side).
When trying to hydrate it using partial query NOTICE is outputted.

Notice: undefined index [*FK column_name of OneToOne assoc*] in UnitOfWork on line: 1773

Probably, the reason is in the way Query calls hydrator's iterate method (with no hints passed to it). Therefore the UnitOfWork object is not affected by HINTFORCE_PARTIALLOAD hint set to the query object.

Suggested solution:
pass _hints array to a hydrator::iterate method when calling it from an AbstractQuery::iterate method.

public function iterate(array $params = array(), $hydrationMode = self::HYDRATE_OBJECT)
    {
        return $this->*em->newHydrator($this->*hydrationMode)->iterate(
            $this->*doExecute($params, $hydrationMode), $this->_resultSetMapping, /* HERE IT IS ----> */ $this->*hints /** <-------- **/
        );
    }

Am I right?

@doctrinebot

Comment created by romanb:

Can we see the code you're using? We need to create a testcase first before fixing it.

The code example in the documentation is no longer correct. I updated it already but the docs did not regenerate yet.

For partial objects, you need to use "select partial u.{id,name} from User u".

The HINTFORCE_PARTIALLOAD now actually means "no proxies" or "optimize everything possible". It really needs to be renamed :)

@doctrinebot

Comment created by 7heaven:

"select partial u.{id,name} from User u"
Use of PARTIAL keyword is possible for DQL queries. How it could be done using native sql queries?

Here is a reproducible test case using sandbox data model:

$userA = new \Entities\User();
$userA->setName("user A");
$userA->setTest("test A");
$em->persist($userA);

$em->flush();
$em->clear();


$sql = "SELECT u.id, u.name, u.test FROM users u";
$rsMapping = new \Doctrine\ORM\Query\ResultSetMapping();
$rsMapping->addEntityResult("Entities\User", "u");
$rsMapping->addFieldResult("u", "id", "id");
$rsMapping->addFieldResult("u", "name", "name");
$rsMapping->addFieldResult("u", "test", "test");

$query = $em->createNativeQuery($sql, $rsMapping);

// Without fix in AbstractQuery::iterate() described above this is of no use
$query->setHint(\Doctrine\ORM\Query::HINT*FORCE_PARTIAL*LOAD, true);

$iterableResult = $query->iterate();
foreach ($iterableResult as $row) {
    $user = $row[0];
    echo "<pre>";
    \Doctrine\Common\Util\Debug::dump($user);
    echo "</pre>";
    $em->remove($user);
}

$em->flush();

Listed code outputs this:

Notice: Undefined index: address_id in *****\Doctrine\ORM\UnitOfWork.php on line 1773

object(stdClass)#66 (5) {
  ["*_CLASS_*"]=>
  string(13) "Entities\User"
  ["id"]=>
  int(29)
  ["name"]=>
  string(6) "user A"
  ["test"]=>
  string(6) "test A"
  ["address"]=>
  NULL
}
@doctrinebot

Comment created by 7heaven:

Improved formatting and code highlighting.

@doctrinebot

Comment created by @beberlei:

Fixed

@doctrinebot

Issue was closed with resolution "Fixed"

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.0-BETA2 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.