Sunday 18 October 2020

Doctrine fetch eagrly does not keep track of referenced entity

I am using doctrine 2.7 with asUnbuffered, due to very large result set. I want to query an entity with a relation to another entity (many to one). My code looks something like this:

    $em = new EntityManager();
    $em->asUnbuffered('connectionId');
    $queryBuilder = $em->createQueryBuilder();
    $queryBuilder
        ->select(Employee::class)
        ->from(…)
        ->where(…)
        ->setParameter(…);
    $query = $queryBuilder->getQuery();
    $query->setFetchMode(Employee::class, 'department', ClassMetadataInfo::FETCH_EAGER);
    foreach ($query->iterate() as $row) {
        yield $row[0];
        $em->clear();
    }

Unfortunately, the Department entity is not being loaded on each iteration. I expect it to be loaded since I use FETCH_EAGER. So for example when I call an inner method of Department, e.g. Department->getId(), I get an error like this: Cannot execute queries while other unbuffered queries are active..

I found a bypass, which is to use the deprecated method $em->detach() instead of $em->clear(). I assume this only detaches the Employee entity but not the Department. But if the Department should be fetched on each iteration, why does $em->clear() not working?

I would like to understand the mechanism, and not just to have something to copy-paste into my code.



from Doctrine fetch eagrly does not keep track of referenced entity

No comments:

Post a Comment