Skip to content

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

@insekticid

Description

@insekticid
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
#7860

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions