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

Open
opened 2026-01-22 13:27:42 +01:00 by admin · 2 comments
Owner

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

Originally assigned to: @beberlei on GitHub.

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

Originally created by @doctrinebot on GitHub (Nov 2, 2011). Originally assigned to: @beberlei on GitHub. 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
admin added the Bug label 2026-01-22 13:27:42 +01:00
Author
Owner
@doctrinebot commented on GitHub (Nov 2, 2011): - duplicates [DDC-624: Partial object query that leaves out an association to avoid loading it fetches the association anyway.](http://www.doctrine-project.org/jira/browse/DDC-624)
Author
Owner

@Amunak commented on GitHub (Mar 2, 2019):

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.

@Amunak commented on GitHub (Mar 2, 2019): 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](https://github.com/doctrine/orm/blob/e8eaf8386de1497863cb400fbc4fddda5878db7e/lib/Doctrine/ORM/Query/SqlWalker.php#L1415) like so: ```patch --- 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1837