From 2845cd37565551be0cd6d7920e9428c9e82149c3 Mon Sep 17 00:00:00 2001 From: Louis Fortunier Date: Wed, 17 Jun 2026 14:34:00 +0200 Subject: [PATCH] Add automatic history reset on entity archive. --- CHANGELOG.md | 4 ++++ src/Entity/Log/HistorizableInterface.php | 4 ++++ src/Entity/Log/HistorizableTrait.php | 12 ++++++++++++ src/Logger/HistoryLogger.php | 9 +++++++++ 4 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e3288a..3abc734 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ CHANGELOG for 1.x =================== +## v1.19.0 - (2026-06-19) +### Added +- Automatic history reset on entity archive. If some archive behaviors in your project should not trigger this reset, override the `resetOnArchive` method on the relevant entity. + ## v1.18.2 - (2026-06-12) ### Removed - Monitoring route `/phpinfo` removed because of security concern diff --git a/src/Entity/Log/HistorizableInterface.php b/src/Entity/Log/HistorizableInterface.php index fe25daf..8f4c356 100644 --- a/src/Entity/Log/HistorizableInterface.php +++ b/src/Entity/Log/HistorizableInterface.php @@ -28,4 +28,8 @@ public function isDoctrineListenerEnable(): bool; * ] */ public function getHistoryDiffFieldsToSkip(): array; + + public function resetHistory(): self; + + public function resetOnArchive(): bool; } diff --git a/src/Entity/Log/HistorizableTrait.php b/src/Entity/Log/HistorizableTrait.php index 3ca1c60..49bec5f 100644 --- a/src/Entity/Log/HistorizableTrait.php +++ b/src/Entity/Log/HistorizableTrait.php @@ -47,4 +47,16 @@ public function getHistoryDiffFieldsToSkip(): array { return []; } + + public function resetHistory(): self + { + $this->setHistory(null); + + return $this; + } + + public function resetOnArchive(): bool + { + return true; + } } diff --git a/src/Logger/HistoryLogger.php b/src/Logger/HistoryLogger.php index d92b768..15579fc 100644 --- a/src/Logger/HistoryLogger.php +++ b/src/Logger/HistoryLogger.php @@ -100,6 +100,15 @@ public function isFlushLog(): bool */ public function log(HistorizableInterface $entity, ?string $code = null, array $data = []): void { + if ($code === self::ARCHIVED_CODE && $entity->resetOnArchive()) { + $entity->resetHistory(); + if ($this->flushLog) { + $this->entityManager->flush(); + } + + return; + } + $history = array_merge([self::DATE_PROPERTY => time()], $data); foreach ( [