doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.93k stars 2.51k forks source link

DDC-1465: Fetching partial objects doesn't work if HINT_FORCE_PARTIAL_LOAD is not explicitly used #2094

Open doctrinebot opened 13 years ago

doctrinebot commented 13 years ago

Jira issue originally created by user jpauli:

Using the DQL "partial" keyword is not enough to get a partial entity as a result. The DQL hint HINT_FORCE_PARTIAL_LOAD must be used as well.

$q = $em->createQuery('SELECT partial r.{id,comment} FROM Entities\Rating r WHERE r.id=3');
$r = $q->getResult() /** HYDRATE_OBJECT is the default hydration mode **/

Here, $r contains the full Entity, a SELECT * has been sent

$q = $em->createQuery('SELECT partial r.{id,comment} FROM Entities\Rating r WHERE r.id=3');
$q->setHint(Doctrine\ORM\Query::HINT*FORCE_PARTIAL*LOAD, 1);

$r = $q->getResult() /** HYDRATE_OBJECT is the default hydration mode **/

Here, $r contains only the selected fields, hence a true partial Entity

doctrinebot commented 13 years ago
Amunak commented 5 years ago

I have no idea whether this is the correct way to fix this bug as I'm not really familiar with the Doctrine codebase, but adding the query hint to Doctrine\ORM\Query\SqlWalker::walkSelectExpression somewhere inside this condition like so:

--- a/lib/Doctrine/ORM/Query/SqlWalker.php
+++ b/lib/Doctrine/ORM/Query/SqlWalker.php
@@ -1413,9 +1413,10 @@
 default:
     // IdentificationVariable or PartialObjectExpression
     if ($expr instanceof AST\PartialObjectExpression) {
         $dqlAlias        = $expr->identificationVariable;
         $partialFieldSet = $expr->partialFieldSet;
+        $this->query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, 1);
     } else {
         $dqlAlias        = $expr;
         $partialFieldSet = [];
     }

seems to do the job fairly well. At least for the most common case of using a partial query.