Skip to content

Commit 50ba6ed

Browse files
Ensure objects that are proxies are properly initialized before
deletions
1 parent e9f8518 commit 50ba6ed

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/EventListener/StorageListener.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\Common\EventSubscriber;
66
use Doctrine\ORM\Event\LifecycleEventArgs;
77
use Doctrine\ORM\Event\PostFlushEventArgs;
8+
use Doctrine\ORM\Proxy\Proxy;
89
use League\Flysystem\FileNotFoundException;
910
use Opensoft\StorageBundle\Entity\StorageFile;
1011
use Opensoft\StorageBundle\Storage\StorageManagerInterface;
@@ -42,6 +43,23 @@ public function __construct(StorageManagerInterface $storageManager, LoggerInter
4243
$this->logger = $logger;
4344
}
4445

46+
/**
47+
* Ensure removed entities have been properly proxy loaded so that deleteEntityFromStorage works
48+
*
49+
* @param LifecycleEventArgs $eventArgs
50+
*/
51+
public function preRemove(LifecycleEventArgs $eventArgs): void
52+
{
53+
$entity = $eventArgs->getEntity();
54+
if ($entity instanceof StorageFile) {
55+
// doctrine <= 3 (doctrine switches to ocramius/proxy-manager in version 3)
56+
if ($entity instanceof Proxy) {
57+
// manually trigger a proxy load by asking for one of the class members
58+
$entity->getStorage();
59+
}
60+
}
61+
}
62+
4563
/**
4664
* Remove is called after each DELETE statement inside the transaction is sent to the DB
4765
*
@@ -111,8 +129,9 @@ private function deleteEntityFromStorage(StorageFile $file): void
111129
public function getSubscribedEvents(): array
112130
{
113131
return [
114-
'postFlush',
115-
'postRemove'
132+
'preRemove',
133+
'postRemove',
134+
'postFlush'
116135
];
117136
}
118137
}

0 commit comments

Comments
 (0)