Skip to content

Commit

Permalink
Add HistorizableInterface::getHistoryDiffFieldsToSkip to manage field…
Browse files Browse the repository at this point in the history
…s to skip logging on history
  • Loading branch information
mathieu-ducrot committed Aug 28, 2024
1 parent cf5f118 commit b2bcc63
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Entity/Log/HistorizableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ public function addHistory(array $history): self;
* Allows you to activate or not the history on prePersist/preUpdate doctrine events for diffs
*/
public function isDoctrineListenerEnable(): bool;

/**
* @return array Indexed array which contains field to skip storing on the history JSON by the HistoryDoctrineListener when doing update
* Example :
* [
* "progressionData" => true,
* ]
*/
public function getHistoryDiffFieldsToSkip(): array;
}
5 changes: 5 additions & 0 deletions src/Entity/Log/HistorizableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public function isDoctrineListenerEnable(): bool
{
return true;
}

public function getHistoryDiffFieldsToSkip(): array
{
return [];
}
}
9 changes: 8 additions & 1 deletion src/EventListener/HistoryDoctrineListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ private function handleHistory(LifecycleEventArgs $args, string $code): void

$statusDiff = null;
$isHistorizableStatus = $entity instanceof HistorizableStatusInterface;
$diffFieldsToSkip = $entity->getHistoryDiffFieldsToSkip();
foreach ($entityData as $field => $change) {
if ($field === 'history' || $field === 'updatedAt' || $field === 'updatedAtMonth' || $field === 'updatedAtYear') {
if (
$field === 'history'
|| $field === 'updatedAt'
|| $field === 'updatedAtMonth'
|| $field === 'updatedAtYear'
|| isset($diffFieldsToSkip[$field])
) {
unset($entityData[$field]);
continue;
}
Expand Down

0 comments on commit b2bcc63

Please sign in to comment.