From 4a18f83c69d2b9023352c94a8f11e59c9889df5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ludvik?= Date: Mon, 9 Sep 2024 13:19:13 +0200 Subject: [PATCH] [framework] fixed EntityLogger to no longer empty collection that is cleared and filled on every update (#3406) --- .../EventListener/EntityLogEventListener.php | 23 +++++++++---------- src/Resources/config/services.yaml | 10 ++++++++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Component/EntityLog/EventListener/EntityLogEventListener.php b/src/Component/EntityLog/EventListener/EntityLogEventListener.php index 7812637966..ca65513f2e 100644 --- a/src/Component/EntityLog/EventListener/EntityLogEventListener.php +++ b/src/Component/EntityLog/EventListener/EntityLogEventListener.php @@ -27,14 +27,16 @@ class EntityLogEventListener implements ResetInterface protected array $logs = []; /** - * @param \Doctrine\ORM\EntityManagerInterface $em + * @param \Doctrine\ORM\EntityManagerInterface $entityLogEntityManager + * @param \Doctrine\ORM\EntityManagerInterface $applicationEntityManager * @param \Psr\Log\LoggerInterface $monolog * @param \Shopsys\FrameworkBundle\Component\EntityLog\Attribute\LoggableEntityConfigFactory $loggableEntityConfigFactory * @param \Shopsys\FrameworkBundle\Component\EntityLog\ChangeSet\ChangeSetResolver $changeSetResolver * @param \Shopsys\FrameworkBundle\Component\EntityLog\Model\EntityLogFacade $entityLogFacade */ public function __construct( - protected readonly EntityManagerInterface $em, + protected readonly EntityManagerInterface $entityLogEntityManager, + protected readonly EntityManagerInterface $applicationEntityManager, protected readonly LoggerInterface $monolog, protected readonly LoggableEntityConfigFactory $loggableEntityConfigFactory, protected readonly ChangeSetResolver $changeSetResolver, @@ -55,14 +57,13 @@ public function postFlush(PostFlushEventArgs $args): void foreach ($this->logs as $log) { $log->setLogCollectionNumber($logCollectionNumber); - $this->em->persist($log); + $this->entityLogEntityManager->persist($log); } $this->logs = []; - $this->em->flush(); + $this->entityLogEntityManager->flush(); } - //create /** * @param \Doctrine\ORM\Event\PostPersistEventArgs $args */ @@ -72,7 +73,6 @@ public function postPersist(PostPersistEventArgs $args): void $this->log(EntityLogActionEnum::CREATE, $entity); } - //update /** * @param \Doctrine\ORM\Event\PostUpdateEventArgs $args */ @@ -82,7 +82,6 @@ public function postUpdate(PostUpdateEventArgs $args): void $this->log(EntityLogActionEnum::UPDATE, $entity); } - //delete /** * @param \Doctrine\ORM\Event\PreRemoveEventArgs $args */ @@ -100,11 +99,11 @@ protected function log(string $action, object $entity): void { $loggableSetup = $this->loggableEntityConfigFactory->getLoggableSetupByEntity($entity); - try { - if (!$loggableSetup->isLoggable()) { - return; - } + if (!$loggableSetup->isLoggable()) { + return; + } + try { $this->registerLog($entity, $loggableSetup, $action); } catch (Throwable $exception) { $this->monolog->error($exception->getMessage()); @@ -141,7 +140,7 @@ protected function registerLog( protected function resolveUpdateChangeSet(object $entity): array { $resolvedChangeSet = []; - $unitOfWork = $this->em->getUnitOfWork(); + $unitOfWork = $this->applicationEntityManager->getUnitOfWork(); $scheduledCollectionUpdates = $unitOfWork->getScheduledCollectionUpdates(); diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 0adb4ad9f2..1c63378f40 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -37,6 +37,14 @@ services: - '@doctrine.dbal.default_connection' - '@doctrine.orm.default_configuration' + doctrine.orm.entity_logging: + class: Doctrine\ORM\EntityManager + factory: [Doctrine\ORM\EntityManager, create] + public: true + arguments: + - '@doctrine.dbal.default_connection' + - '@doctrine.orm.default_configuration' + doctrine.orm.entity_manager.abstract: synthetic: true public: true @@ -1045,6 +1053,8 @@ services: $resettableServices: !tagged kernel.reset Shopsys\FrameworkBundle\Component\EntityLog\EventListener\EntityLogEventListener: + arguments: + $entityLogEntityManager: '@doctrine.orm.entity_logging' tags: - { name: doctrine.event_listener, event: postPersist, priority: 1 } - { name: doctrine.event_listener, event: postUpdate, priority: 1 }