From 0327da154e93c4be73e91d2c223b3d45639e2fbd Mon Sep 17 00:00:00 2001 From: Ilaria Orlando Date: Thu, 28 May 2026 16:27:23 +0200 Subject: [PATCH] flush inside repo's instead of inside the message handlers --- .../Command/CopyContentBlocksToOtherLocaleHandler.php | 3 --- .../ContentBlock/Command/CreateContentBlockHandler.php | 3 --- .../ContentBlock/Command/DeleteContentBlockHandler.php | 3 --- .../ContentBlock/Command/UpdateContentBlockHandler.php | 3 --- .../Domain/ContentBlock/ContentBlockRepository.php | 10 ++++++++-- .../MediaGallery/Command/CreateMediaGalleryHandler.php | 3 --- .../MediaGallery/Command/DeleteMediaGalleryHandler.php | 3 --- .../MediaGallery/Command/UpdateMediaGalleryHandler.php | 5 +++-- .../Domain/MediaGallery/MediaGalleryRepository.php | 7 +++++++ .../MediaFolder/Command/CreateMediaFolderHandler.php | 3 --- .../MediaFolder/Command/DeleteMediaFolderHandler.php | 2 -- .../MediaFolder/Command/UpdateMediaFolderHandler.php | 5 +++-- .../Domain/MediaFolder/MediaFolderRepository.php | 9 +++++++-- 13 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/CopyContentBlocksToOtherLocaleHandler.php b/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/CopyContentBlocksToOtherLocaleHandler.php index 779f9ed61b..24a1e7b38a 100644 --- a/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/CopyContentBlocksToOtherLocaleHandler.php +++ b/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/CopyContentBlocksToOtherLocaleHandler.php @@ -16,7 +16,6 @@ { public function __construct( private ContentBlockRepository $contentBlockRepository, - private EntityManagerInterface $entityManager, ) { } @@ -41,8 +40,6 @@ function (ContentBlock $contentBlock) use ($copyContentBlocksToOtherLocale, &$id }, $contentBlocksToCopy ); - - $this->entityManager->flush(); } private function getContentBlocksToCopy(Locale $locale): array diff --git a/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/CreateContentBlockHandler.php b/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/CreateContentBlockHandler.php index 13b5804bc7..2a52690f2b 100644 --- a/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/CreateContentBlockHandler.php +++ b/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/CreateContentBlockHandler.php @@ -14,7 +14,6 @@ { public function __construct( private ContentBlockRepository $contentBlockRepository, - private EntityManagerInterface $entityManager, ) { } @@ -27,8 +26,6 @@ public function __invoke(CreateContentBlock $createContentBlock): void $this->contentBlockRepository->add($contentBlock); $createContentBlock->setContentBlockEntity($contentBlock); - - $this->entityManager->flush(); } private function getNewExtraId(): int diff --git a/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/DeleteContentBlockHandler.php b/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/DeleteContentBlockHandler.php index a57926a0ce..5d370625ee 100644 --- a/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/DeleteContentBlockHandler.php +++ b/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/DeleteContentBlockHandler.php @@ -12,7 +12,6 @@ { public function __construct( private ContentBlockRepository $contentBlockRepository, - private EntityManagerInterface $entityManager, ) { } @@ -24,7 +23,5 @@ public function __invoke(DeleteContentBlock $deleteContentBlock): void ); Model::deleteExtraById($deleteContentBlock->contentBlock->getExtraId()); - - $this->entityManager->flush(); } } diff --git a/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/UpdateContentBlockHandler.php b/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/UpdateContentBlockHandler.php index 533189ae3d..6bdb552d05 100644 --- a/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/UpdateContentBlockHandler.php +++ b/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/Command/UpdateContentBlockHandler.php @@ -12,7 +12,6 @@ { public function __construct( private ContentBlockRepository $contentBlockRepository, - private EntityManagerInterface $entityManager, ) { } @@ -22,7 +21,5 @@ public function __invoke(UpdateContentBlock $updateContentBlock): void $this->contentBlockRepository->add($contentBlock); $updateContentBlock->setContentBlockEntity($contentBlock); - - $this->entityManager->flush(); } } diff --git a/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/ContentBlockRepository.php b/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/ContentBlockRepository.php index 4fe3a1ad61..4a00831daa 100644 --- a/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/ContentBlockRepository.php +++ b/src/Backend/Modules/ContentBlocks/Domain/ContentBlock/ContentBlockRepository.php @@ -20,8 +20,8 @@ function (ContentBlock $contentBlock): void { ); } - // We don't flush here, see http://disq.us/p/okjc6b $this->getEntityManager()->persist($contentBlock); + $this->save(); } public function getNextIdForLanguage(Locale $locale): int @@ -72,12 +72,18 @@ public function findOneByRevisionIdAndLocale(?int $revisionId, Locale $locale): public function removeByIdAndLocale($id, Locale $locale): void { - // We don't flush here, see http://disq.us/p/okjc6b array_map( function (ContentBlock $contentBlock): void { $this->getEntityManager()->remove($contentBlock); }, (array) $this->findBy(['id' => $id, 'locale' => $locale]) ); + + $this->save(); + } + + public function save(): void + { + $this->getEntityManager()->flush(); } } diff --git a/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/Command/CreateMediaGalleryHandler.php b/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/Command/CreateMediaGalleryHandler.php index a62a7ccbe6..410a2ff3e0 100644 --- a/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/Command/CreateMediaGalleryHandler.php +++ b/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/Command/CreateMediaGalleryHandler.php @@ -12,7 +12,6 @@ { public function __construct( private MediaGalleryRepository $mediaGalleryRepository, - private EntityManagerInterface $entityManager, ) { } @@ -23,7 +22,5 @@ public function __invoke(CreateMediaGallery $createMediaGallery): void // We redefine the mediaGallery, so we can use it in an action $createMediaGallery->setMediaGalleryEntity($mediaGallery); - - $this->entityManager->flush(); } } diff --git a/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/Command/DeleteMediaGalleryHandler.php b/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/Command/DeleteMediaGalleryHandler.php index 7a046b82e7..d431a9405e 100644 --- a/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/Command/DeleteMediaGalleryHandler.php +++ b/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/Command/DeleteMediaGalleryHandler.php @@ -15,7 +15,6 @@ public function __construct( private MediaGalleryRepository $mediaGalleryRepository, private MediaItemRepository $mediaItemRepository, - private EntityManagerInterface $entityManager, ) { } @@ -34,7 +33,5 @@ public function __invoke(DeleteMediaGallery $deleteMediaGallery): void } $this->mediaGalleryRepository->remove($deleteMediaGallery->mediaGallery); - - $this->entityManager->flush(); } } diff --git a/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/Command/UpdateMediaGalleryHandler.php b/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/Command/UpdateMediaGalleryHandler.php index a9d434b1f6..dc50c6afe2 100644 --- a/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/Command/UpdateMediaGalleryHandler.php +++ b/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/Command/UpdateMediaGalleryHandler.php @@ -3,6 +3,7 @@ namespace Backend\Modules\MediaGalleries\Domain\MediaGallery\Command; use Backend\Modules\MediaGalleries\Domain\MediaGallery\MediaGallery; +use Backend\Modules\MediaGalleries\Domain\MediaGallery\MediaGalleryRepository; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Messenger\Attribute\AsMessageHandler; @@ -10,7 +11,7 @@ final readonly class UpdateMediaGalleryHandler { public function __construct( - private EntityManagerInterface $entityManager, + private MediaGalleryRepository $mediaGalleryRepository, ) { } @@ -19,6 +20,6 @@ public function __invoke(UpdateMediaGallery $updateMediaGallery): void // We redefine the mediaGallery, so we can use it in an action $updateMediaGallery->setMediaGalleryEntity(MediaGallery::fromDataTransferObject($updateMediaGallery)); - $this->entityManager->flush(); + $this->mediaGalleryRepository->save(); } } diff --git a/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/MediaGalleryRepository.php b/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/MediaGalleryRepository.php index a0c708a71c..60c94b2aa5 100644 --- a/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/MediaGalleryRepository.php +++ b/src/Backend/Modules/MediaGalleries/Domain/MediaGallery/MediaGalleryRepository.php @@ -16,6 +16,7 @@ final class MediaGalleryRepository extends EntityRepository public function add(MediaGallery $mediaGallery): void { $this->getEntityManager()->persist($mediaGallery); + $this->save(); } public function existsByTitle(string $title, ?string $ignoreMediaGalleryId = null): bool @@ -47,5 +48,11 @@ public function findOneById(?string $id = null): MediaGallery public function remove(MediaGallery $mediaGallery): void { $this->getEntityManager()->remove($mediaGallery); + $this->save(); + } + + public function save(): void + { + $this->getEntityManager()->flush(); } } diff --git a/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/Command/CreateMediaFolderHandler.php b/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/Command/CreateMediaFolderHandler.php index c067e6785a..79c519eebc 100644 --- a/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/Command/CreateMediaFolderHandler.php +++ b/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/Command/CreateMediaFolderHandler.php @@ -12,7 +12,6 @@ { public function __construct( private MediaFolderRepository $mediaFolderRepository, - private EntityManagerInterface $entityManager, ) { } @@ -24,7 +23,5 @@ public function __invoke(CreateMediaFolder $createMediaFolder): void // We redefine the MediaFolder, so we can use it in an action $createMediaFolder->setMediaFolderEntity($mediaFolder); - - $this->entityManager->flush(); } } diff --git a/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/Command/DeleteMediaFolderHandler.php b/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/Command/DeleteMediaFolderHandler.php index 8646916af4..3e7e4ecf94 100644 --- a/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/Command/DeleteMediaFolderHandler.php +++ b/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/Command/DeleteMediaFolderHandler.php @@ -20,7 +20,5 @@ public function __invoke(DeleteMediaFolder $deleteMediaFolder): void $mediaFolder = $deleteMediaFolder->mediaFolder; $this->mediaFolderRepository->remove($mediaFolder); - - $this->entityManager->flush(); } } diff --git a/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/Command/UpdateMediaFolderHandler.php b/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/Command/UpdateMediaFolderHandler.php index 3fa0e786d2..bce4ba8248 100644 --- a/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/Command/UpdateMediaFolderHandler.php +++ b/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/Command/UpdateMediaFolderHandler.php @@ -3,6 +3,7 @@ namespace Backend\Modules\MediaLibrary\Domain\MediaFolder\Command; use Backend\Modules\MediaLibrary\Domain\MediaFolder\MediaFolder; +use Backend\Modules\MediaLibrary\Domain\MediaFolder\MediaFolderRepository; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Messenger\Attribute\AsMessageHandler; @@ -10,7 +11,7 @@ final readonly class UpdateMediaFolderHandler { public function __construct( - private EntityManagerInterface $entityManager, + private MediaFolderRepository $mediaFolderRepository, ) { } @@ -19,6 +20,6 @@ public function __invoke(UpdateMediaFolder $updateMediaFolder): void // We redefine the MediaFolder, so we can use it in an action $updateMediaFolder->setMediaFolderEntity(MediaFolder::fromDataTransferObject($updateMediaFolder)); - $this->entityManager->flush(); + $this->mediaFolderRepository->save(); } } diff --git a/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/MediaFolderRepository.php b/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/MediaFolderRepository.php index 688fbe0e88..a1b4bd3766 100644 --- a/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/MediaFolderRepository.php +++ b/src/Backend/Modules/MediaLibrary/Domain/MediaFolder/MediaFolderRepository.php @@ -10,8 +10,8 @@ final class MediaFolderRepository extends EntityRepository { public function add(MediaFolder $mediaFolder): void { - // We don't flush here, see http://disq.us/p/okjc6b $this->getEntityManager()->persist($mediaFolder); + $this->save(); } private function bumpFolderCount(int $folderId, array &$counts): void @@ -82,7 +82,12 @@ public function getCountsForMediaGroup(MediaGroup $mediaGroup): array public function remove(MediaFolder $mediaFolder): void { - // We don't flush here, see http://disq.us/p/okjc6b $this->getEntityManager()->remove($mediaFolder); + $this->save(); + } + + public function save(): void + { + $this->getEntityManager()->flush(); } }