Skip to content

Commit

Permalink
[framework] fixed EntityLogger to no longer empty collection that is …
Browse files Browse the repository at this point in the history
…cleared and filled on every update (#3406)
  • Loading branch information
TomasLudvik authored Sep 9, 2024
1 parent 923de7a commit 4a18f83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/Component/EntityLog/EventListener/EntityLogEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
*/
Expand All @@ -72,7 +73,6 @@ public function postPersist(PostPersistEventArgs $args): void
$this->log(EntityLogActionEnum::CREATE, $entity);
}

//update
/**
* @param \Doctrine\ORM\Event\PostUpdateEventArgs $args
*/
Expand All @@ -82,7 +82,6 @@ public function postUpdate(PostUpdateEventArgs $args): void
$this->log(EntityLogActionEnum::UPDATE, $entity);
}

//delete
/**
* @param \Doctrine\ORM\Event\PreRemoveEventArgs $args
*/
Expand All @@ -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());
Expand Down Expand Up @@ -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();

Expand Down
10 changes: 10 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand Down

0 comments on commit 4a18f83

Please sign in to comment.