Skip to content

Commit 83bae60

Browse files
fix(symfony): detach objects to prevent loop when using Doctrine middleware and Mercure (#6936) (#6965)
1 parent 3a036dc commit 83bae60

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

Doctrine/EventListener/PublishMercureUpdatesListener.php

+19-7
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,29 @@ public function onFlush(EventArgs $eventArgs): void
131131
public function postFlush(): void
132132
{
133133
try {
134-
foreach ($this->createdObjects as $object) {
135-
$this->publishUpdate($object, $this->createdObjects[$object], 'create');
134+
$creatingObjects = clone $this->createdObjects;
135+
foreach ($creatingObjects as $object) {
136+
if ($this->createdObjects->contains($object)) {
137+
$this->createdObjects->detach($object);
138+
}
139+
$this->publishUpdate($object, $creatingObjects[$object], 'create');
136140
}
137141

138-
foreach ($this->updatedObjects as $object) {
139-
$this->publishUpdate($object, $this->updatedObjects[$object], 'update');
142+
$updatingObjects = clone $this->updatedObjects;
143+
foreach ($updatingObjects as $object) {
144+
if ($this->updatedObjects->contains($object)) {
145+
$this->updatedObjects->detach($object);
146+
}
147+
$this->publishUpdate($object, $updatingObjects[$object], 'update');
140148
}
141149

142-
foreach ($this->deletedObjects as $object) {
143-
$this->publishUpdate($object, $this->deletedObjects[$object], 'delete');
150+
$deletingObjects = clone $this->deletedObjects;
151+
foreach ($deletingObjects as $object) {
152+
$options = $this->deletedObjects[$object];
153+
if ($this->deletedObjects->contains($object)) {
154+
$this->deletedObjects->detach($object);
155+
}
156+
$this->publishUpdate($object, $deletingObjects[$object], 'delete');
144157
}
145158
} finally {
146159
$this->reset();
@@ -237,7 +250,6 @@ private function publishUpdate(object $object, array $options, string $type): vo
237250
}
238251

239252
$updates = array_merge([$this->buildUpdate($iri, $data, $options)], $this->getGraphQlSubscriptionUpdates($object, $options, $type));
240-
241253
foreach ($updates as $update) {
242254
if ($options['enable_async_update'] && $this->messageBus) {
243255
$this->dispatch($update);

0 commit comments

Comments
 (0)