From c62ffae78f50d09317750b09f4b80818a8420956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C4=81vis=20Z=C4=81l=C4=ABtis?= Date: Tue, 11 Apr 2023 17:45:50 +0300 Subject: [PATCH 1/3] fix: change getEntityChangeSet back to getOriginalEntityData --- src/EventSubscriber/AuditDoctrineEventSubscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventSubscriber/AuditDoctrineEventSubscriber.php b/src/EventSubscriber/AuditDoctrineEventSubscriber.php index 6549800..be5dd18 100755 --- a/src/EventSubscriber/AuditDoctrineEventSubscriber.php +++ b/src/EventSubscriber/AuditDoctrineEventSubscriber.php @@ -69,7 +69,7 @@ private function logActivity(string $action, LifecycleEventArgs $args): void } $entityManager = $args->getObjectManager(); - $originalEntityData = $entityManager->getUnitOfWork()->getEntityChangeSet($entity); + $originalEntityData = $entityManager->getUnitOfWork()->getOriginalEntityData($entity); if (!array_key_exists('id', $originalEntityData)) { $originalEntityData['id'] = $entity->getId(); } From 85fa7d31f7240b1befc2891723971b49a3cc7fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C4=81vis=20Z=C4=81l=C4=ABtis?= Date: Wed, 12 Apr 2023 11:43:47 +0300 Subject: [PATCH 2/3] fix: rework postUpdate event logic --- .../AuditDoctrineEventSubscriber.php | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/EventSubscriber/AuditDoctrineEventSubscriber.php b/src/EventSubscriber/AuditDoctrineEventSubscriber.php index be5dd18..9583d23 100755 --- a/src/EventSubscriber/AuditDoctrineEventSubscriber.php +++ b/src/EventSubscriber/AuditDoctrineEventSubscriber.php @@ -44,20 +44,20 @@ public function getSubscribedEvents(): array public function postPersist(LifecycleEventArgs $args): void { - $this->logActivity($this->translator->trans('entity.create', domain: 'Audit'), $args); + $this->logActivity(Events::postPersist, $this->translator->trans('entity.create', domain: 'Audit'), $args); } public function preRemove(LifecycleEventArgs $args): void { - $this->logActivity($this->translator->trans('entity.remove', domain: 'Audit'), $args); + $this->logActivity(Events::preRemove, $this->translator->trans('entity.remove', domain: 'Audit'), $args); } public function postUpdate(LifecycleEventArgs $args): void { - $this->logActivity($this->translator->trans('entity.update', domain: 'Audit'), $args); + $this->logActivity(Events::postUpdate, $this->translator->trans('entity.update', domain: 'Audit'), $args); } - private function logActivity(string $action, LifecycleEventArgs $args): void + private function logActivity(string $event, string $action, LifecycleEventArgs $args): void { if (!$this->isEnabled) { return; @@ -69,18 +69,30 @@ private function logActivity(string $action, LifecycleEventArgs $args): void } $entityManager = $args->getObjectManager(); - $originalEntityData = $entityManager->getUnitOfWork()->getOriginalEntityData($entity); + if (Events::postUpdate === $event) { + $originalEntityData = $entityManager->getUnitOfWork()->getEntityChangeSet($entity); + $entityData = []; + foreach ($originalEntityData as $field => $value) { + $entityData[$field] = [ + $this->normalizeEntityCollections($value), + ]; + } + $originalEntityData = $entityData; + } else { + $originalEntityData = $entityManager->getUnitOfWork()->getOriginalEntityData($entity); + $originalEntityData = $this->normalizeEntityCollections($originalEntityData); + } + if (!array_key_exists('id', $originalEntityData)) { $originalEntityData['id'] = $entity->getId(); } - $originalEntityData = $this->normalizeEntityCollections($originalEntityData); $this->audit->audit(AuditType::DB, sprintf('%s on %s', $action, $entity::class), $originalEntityData); } private function normalizeEntityCollections(mixed $entity): array|int|string { - return array_map(static function ($value) { + return array_map(function ($value) { if ($value instanceof PersistentCollection) { $collectionValue = []; foreach ($value as $item) { From ec5f4c5c887ba4bb3ee033377e3af3128b7b16bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C4=81vis=20Z=C4=81l=C4=ABtis?= Date: Wed, 12 Apr 2023 11:45:01 +0300 Subject: [PATCH 3/3] fix: add back static keyword --- src/EventSubscriber/AuditDoctrineEventSubscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventSubscriber/AuditDoctrineEventSubscriber.php b/src/EventSubscriber/AuditDoctrineEventSubscriber.php index 9583d23..3a3f92c 100755 --- a/src/EventSubscriber/AuditDoctrineEventSubscriber.php +++ b/src/EventSubscriber/AuditDoctrineEventSubscriber.php @@ -92,7 +92,7 @@ private function logActivity(string $event, string $action, LifecycleEventArgs $ private function normalizeEntityCollections(mixed $entity): array|int|string { - return array_map(function ($value) { + return array_map(static function ($value) { if ($value instanceof PersistentCollection) { $collectionValue = []; foreach ($value as $item) {