Issue with loading additional associated entities during postLoad #5302

Closed
opened 2026-01-22 15:03:59 +01:00 by admin · 1 comment
Owner

Originally created by @yellow1912 on GitHub (Oct 26, 2016).

Sorry for the lack of better wording for the question, as I'm not so sure how to describe the problem in a few words.

In my Symfony project with the latest Doctrine (~2.5), to create a form of dynamic association, I need to automatically load additional entities then assign to the current object before I return the object.

A simple version of the code looks like this

public function postLoad(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();

    if (!$entity instanceof ExtendableInterface || !$entity->hasExtension()) {
        return;
    }

    $manager = $args->getEntityManager();
    $accessor = PropertyAccess::createPropertyAccessorBuilder()
        ->enableMagicCall()
        ->getPropertyAccessor()
    ;

    foreach ($entity->getExtension()->getReferences() as $field => $configs) {
        if ($entity->hasField($field) && $data = $entity->getField($field)) {
            $repository = $manager->getRepository($configs['target']);

            if ($configs['options']['multiple']) {
                $criteria = array('id' => $data);
                $resources = $repository->findBy($criteria);
                $accessor->setValue($entity, $field, $resources);
            } else {
                $accessor->setValue($entity, $field, $repository->find($data));
            }
        }
    }
}

In the code, I use the repository to query the additional entities then assign to the current entity. The problem I'm facing is that if these additional entities have association with other entities as well, then these associated fields will not be fetched. When dumped, the "initialized" value is always set to false, and even in the case that I explicitly attempt to fetch the it still doesn't work.

So

$repository->find($data)->getValues() // getValues() access a referenced collection, but in this case it will still return an empty Collection

I'm not sure if it helps but pre-update to Doctrine 2.5 and Symfony 3.x this code used to work.

Originally created by @yellow1912 on GitHub (Oct 26, 2016). Sorry for the lack of better wording for the question, as I'm not so sure how to describe the problem in a few words. In my Symfony project with the latest Doctrine (~2.5), to create a form of dynamic association, I need to automatically load additional entities then assign to the current object before I return the object. A simple version of the code looks like this ``` public function postLoad(LifecycleEventArgs $args) { $entity = $args->getEntity(); if (!$entity instanceof ExtendableInterface || !$entity->hasExtension()) { return; } $manager = $args->getEntityManager(); $accessor = PropertyAccess::createPropertyAccessorBuilder() ->enableMagicCall() ->getPropertyAccessor() ; foreach ($entity->getExtension()->getReferences() as $field => $configs) { if ($entity->hasField($field) && $data = $entity->getField($field)) { $repository = $manager->getRepository($configs['target']); if ($configs['options']['multiple']) { $criteria = array('id' => $data); $resources = $repository->findBy($criteria); $accessor->setValue($entity, $field, $resources); } else { $accessor->setValue($entity, $field, $repository->find($data)); } } } } ``` In the code, I use the repository to query the additional entities then assign to the current entity. The problem I'm facing is that if these additional entities have association with other entities as well, then these associated fields will not be fetched. When dumped, the "initialized" value is always set to false, and even in the case that I explicitly attempt to fetch the it still doesn't work. So `$repository->find($data)->getValues() // getValues() access a referenced collection, but in this case it will still return an empty Collection` I'm not sure if it helps but pre-update to Doctrine 2.5 and Symfony 3.x this code used to work.
admin added the Invalid label 2026-01-22 15:03:59 +01:00
admin closed this issue 2026-01-22 15:03:59 +01:00
Author
Owner

@yellow1912 commented on GitHub (Oct 26, 2016):

Upon further investigation it is actually a logic issue in my code, ___

@yellow1912 commented on GitHub (Oct 26, 2016): Upon further investigation it is actually a logic issue in my code, ___
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5302