Imcomprehensive behaviour with doctrine postRemove events. #6699

Open
opened 2026-01-22 15:37:16 +01:00 by admin · 3 comments
Owner

Originally created by @acantepie on GitHub (Apr 19, 2021).

Hi,

Using doctrine/orm 2.8.4 (got same issue with all 2.8.x tags)

I have an entity :

class UmbrellaFile
{
    /**
     * @var int|null
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @var string
     * @ORM\Column(type="string", length=255, nullable=false)
     */
    public $configName;

 [...] Some other simple properties
}

Somewhere on code, i have following code :
$this->em->remove($file);

If i dump my entity, before call $this->em->remove(...);, i got :

Proxies\__CG__\Umbrella\CoreBundle\Entity\UmbrellaFile^ {#751
  +__isInitialized__: false
  +_uploadedFile: null
  +_deleteSourceFile: true
  +id: 16
   …2
}

I have an EventSubscriberInterface listening on 'postRemove' events:

  public function postRemove(LifecycleEventArgs $args): void
  {
      $entity = $args->getObject();

        if (!$entity instanceof UmbrellaFile) {
            return;
        }
        $entity->configName; // crash here
  }

At runtime got an exception at line $entity->configName:

request.CRITICAL: Uncaught PHP Exception ErrorException: "Warning: Undefined array key "id"" at /home/acantepie/projects/umbrella-admin-demo/vendor/doctrine/orm/lib/Doctrine/ORM/Utility/IdentifierFlattener.php line 94 {"exception":"[object] (ErrorException(code: 0): Warning: Undefined array key \"id\" at /home/acantepie/projects/umbrella-admin-demo/vendor/doctrine/orm/lib/Doctrine/ORM/Utility/IdentifierFlattener.php:94)"} []


in vendor/doctrine/orm/lib/Doctrine/ORM/Utility/IdentifierFlattener.php (line 94)
in vendor/doctrine/orm/lib/Doctrine/ORM/Proxy/ProxyFactory.php -> flattenIdentifier (line 156)
in var/cache/dev/doctrine/orm/Proxies/__CG__UmbrellaCoreBundleEntityUmbrellaFile.php -> Doctrine\ORM\Proxy\{closure} (line 74)
in var/cache/dev/doctrine/orm/Proxies/__CG__UmbrellaCoreBundleEntityUmbrellaFile.php -> __invoke (line 74)
UmbrellaFile->__get('configName')
in /home/acantepie/projects/umbrella/Bundle/CoreBundle/src/Component/UmbrellaFile/UmbrellaFileSubscriber.php (line 34)

I can resolve issue if i force orm to initialize entity :

         $file->configName; // orm will intialize entity
         $this->em->remove($file);
Originally created by @acantepie on GitHub (Apr 19, 2021). Hi, Using doctrine/orm 2.8.4 (got same issue with all 2.8.x tags) I have an entity : ``` class UmbrellaFile { /** * @var int|null * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ public $id; /** * @var string * @ORM\Column(type="string", length=255, nullable=false) */ public $configName; [...] Some other simple properties } ``` Somewhere on code, i have following code : `$this->em->remove($file);` If i dump my entity, before call `$this->em->remove(...);`, i got : ``` Proxies\__CG__\Umbrella\CoreBundle\Entity\UmbrellaFile^ {#751 +__isInitialized__: false +_uploadedFile: null +_deleteSourceFile: true +id: 16 …2 } ``` I have an EventSubscriberInterface listening on 'postRemove' events: ``` public function postRemove(LifecycleEventArgs $args): void { $entity = $args->getObject(); if (!$entity instanceof UmbrellaFile) { return; } $entity->configName; // crash here } ``` At runtime got an exception at line `$entity->configName`: ` request.CRITICAL: Uncaught PHP Exception ErrorException: "Warning: Undefined array key "id"" at /home/acantepie/projects/umbrella-admin-demo/vendor/doctrine/orm/lib/Doctrine/ORM/Utility/IdentifierFlattener.php line 94 {"exception":"[object] (ErrorException(code: 0): Warning: Undefined array key \"id\" at /home/acantepie/projects/umbrella-admin-demo/vendor/doctrine/orm/lib/Doctrine/ORM/Utility/IdentifierFlattener.php:94)"} []` ---- ``` in vendor/doctrine/orm/lib/Doctrine/ORM/Utility/IdentifierFlattener.php (line 94) in vendor/doctrine/orm/lib/Doctrine/ORM/Proxy/ProxyFactory.php -> flattenIdentifier (line 156) in var/cache/dev/doctrine/orm/Proxies/__CG__UmbrellaCoreBundleEntityUmbrellaFile.php -> Doctrine\ORM\Proxy\{closure} (line 74) in var/cache/dev/doctrine/orm/Proxies/__CG__UmbrellaCoreBundleEntityUmbrellaFile.php -> __invoke (line 74) UmbrellaFile->__get('configName') in /home/acantepie/projects/umbrella/Bundle/CoreBundle/src/Component/UmbrellaFile/UmbrellaFileSubscriber.php (line 34) ``` I can resolve issue if i force orm to initialize entity : ``` $file->configName; // orm will intialize entity $this->em->remove($file); ```
Author
Owner

@greg0ire commented on GitHub (Apr 20, 2021):

From the docs:

  Note that the postRemove event or any events triggered after an entity removal can receive an uninitializable proxy in case you have configured an entity to cascade remove relations. In this case, you should load yourself the proxy in the associated pre event.

This sounds a bit like what is happening to you, doesn't it?

@greg0ire commented on GitHub (Apr 20, 2021): From the docs: >   Note that the postRemove event or any events triggered after an entity removal can receive an uninitializable proxy in case you have configured an entity to cascade remove relations. In this case, you should load yourself the proxy in the associated pre event. This sounds a bit like what is happening to you, doesn't it?
Author
Owner

@acantepie commented on GitHub (Apr 20, 2021):

Indeed, but excption isn't comprehensive.

@acantepie commented on GitHub (Apr 20, 2021): Indeed, but excption isn't comprehensive.
Author
Owner

@greg0ire commented on GitHub (Apr 20, 2021):

Careful, "comprehensive" is a faux-ami . But you are right, there could be better DX here.

@greg0ire commented on GitHub (Apr 20, 2021): Careful, "comprehensive" is a _faux-ami_ . But you are right, there could be better DX here.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6699