setFetchMode() is ignored - Exception eager Fetch Join With Not Allowed #7285

Closed
opened 2026-01-22 15:49:10 +01:00 by admin · 0 comments
Owner

Originally created by @insekticid on GitHub (Dec 20, 2023).

Entity/Category.php

#[ORM\Entity]
class Category
{
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    private int $id;

    #[ORM\ManyToOne(targetEntity: 'Category', fetch: 'EAGER')]
    #[ORM\JoinColumn(name: 'root_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
    private ?Category $root = null;
}

Query

$qb = $this->createQueryBuilder('content')
            ->join('content.category', 'category')
            ->join('category.root', 'categoryRoot', Expr\Join::WITH, 'categoryRoot = :root')
            ->setParameter('root', $host->getCategory());

$query = $qb->getQuery();
$query->setFetchMode(Category::class, 'categoryRoot', ClassMetadata::FETCH_LAZY);

Result

Doctrine\ORM\Query\QueryException

Associations with fetch-mode=EAGER may not be using WITH conditions in
             "[App\Entity\Category]"

There should be check over SqlWalker::getQuery()->getHint('fetchMode') in case you override fetchMode()
Maybe there is a bug in $relation->fetch, where fetchMode should be propagated from hints?

if ($relation->fetch === ClassMetadata::FETCH_EAGER && $condExpr !== null) {
            throw QueryException::eagerFetchJoinWithNotAllowed($assoc->sourceEntity, $assoc->fieldName);
}

https://github.com/doctrine/orm/blame/28d03e41f7b58781c516404bf179806e10a84ec5/lib/Doctrine/ORM/Query/SqlWalker.php#L897

Maybe related with
https://github.com/doctrine/orm/issues/7860

Originally created by @insekticid on GitHub (Dec 20, 2023). ``` Entity/Category.php #[ORM\Entity] class Category { #[ORM\Id] #[ORM\Column(type: 'integer')] #[ORM\GeneratedValue(strategy: 'AUTO')] private int $id; #[ORM\ManyToOne(targetEntity: 'Category', fetch: 'EAGER')] #[ORM\JoinColumn(name: 'root_id', referencedColumnName: 'id', onDelete: 'CASCADE')] private ?Category $root = null; } ``` Query ``` $qb = $this->createQueryBuilder('content') ->join('content.category', 'category') ->join('category.root', 'categoryRoot', Expr\Join::WITH, 'categoryRoot = :root') ->setParameter('root', $host->getCategory()); $query = $qb->getQuery(); $query->setFetchMode(Category::class, 'categoryRoot', ClassMetadata::FETCH_LAZY); ``` Result ``` Doctrine\ORM\Query\QueryException Associations with fetch-mode=EAGER may not be using WITH conditions in "[App\Entity\Category]" ``` There should be check over SqlWalker::getQuery()->getHint('fetchMode') in case you override fetchMode() Maybe there is a bug in $relation->fetch, where fetchMode should be propagated from hints? ``` if ($relation->fetch === ClassMetadata::FETCH_EAGER && $condExpr !== null) { throw QueryException::eagerFetchJoinWithNotAllowed($assoc->sourceEntity, $assoc->fieldName); } ``` https://github.com/doctrine/orm/blame/28d03e41f7b58781c516404bf179806e10a84ec5/lib/Doctrine/ORM/Query/SqlWalker.php#L897 Maybe related with https://github.com/doctrine/orm/issues/7860
admin closed this issue 2026-01-22 15:49:10 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7285