Skip to content

Commit

Permalink
fixed custom field name for archived_at
Browse files Browse the repository at this point in the history
  • Loading branch information
StvL97 committed Dec 8, 2023
1 parent 285f324 commit 5c2ec12
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/ArchiveManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,11 @@ private function getTransaction(EntityArchivingConfiguration $configuration): Tr
}

// "archived_at" field may not exist in the original table, so we add it here
$archivingDateFieldName = $configuration->getArchivingDateFieldName();
if ($configuration->isAddArchivedAtField()) {
$configuration->setArchivedFields(array_merge($configuration->getArchivedFields(), ['archived_at']));
$configuration->setArchivedFields(
array_merge($configuration->getArchivedFields(), [$archivingDateFieldName])
);
}

$this->applyFilters($configuration, $query);
Expand All @@ -253,7 +256,7 @@ private function getTransaction(EntityArchivingConfiguration $configuration): Tr
if ($configuration->isAddArchivedAtField()) {
$date = (new DateTime())->format('Y-m-d H:i:s');
foreach ($result as &$item) {
$item['archived_at'] = $date;
$item[$archivingDateFieldName] = $date;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

class Configuration implements ConfigurationInterface
{
private const ARCHIVE_TABLE_SUFFIX_DEFAULT = '_archive';
public const ARCHIVE_TABLE_SUFFIX_DEFAULT = '_archive';

private const ARCHIVING_DATE_FIELD_NAME_DEFAULT = 'archived_at';
public const ARCHIVING_DATE_FIELD_NAME_DEFAULT = 'archived_at';

/**
* @return TreeBuilder
Expand Down
3 changes: 2 additions & 1 deletion src/Model/EntityArchivingConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fastbolt\EntityArchiverBundle\Model;

use Fastbolt\EntityArchiverBundle\DependencyInjection\Configuration;
use Fastbolt\EntityArchiverBundle\Strategy\EntityArchivingStrategy;

class EntityArchivingConfiguration
Expand All @@ -26,7 +27,7 @@ class EntityArchivingConfiguration

private bool $addArchivedAtField = false;

private string $archivingDateFieldName = 'archived_at';
private string $archivingDateFieldName = Configuration::ARCHIVING_DATE_FIELD_NAME_DEFAULT;

/**
* @return string
Expand Down
19 changes: 19 additions & 0 deletions src/Model/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class Transaction
*/
private ClassMetadata $classMetaData;

/**
* Name of the field that holds the archiving date
*
* @var string
*/
private string $archivedAtFieldName;

/**
* @return string
*/
Expand Down Expand Up @@ -215,4 +222,16 @@ public function setClassMetaData(ClassMetadata $classMetaData): Transaction

return $this;
}

public function getArchivedAtFieldName(): string
{
return $this->archivedAtFieldName;
}

public function setArchivedAtFieldName(string $archivedAtFieldName): Transaction
{
$this->archivedAtFieldName = $archivedAtFieldName;

return $this;
}
}
2 changes: 1 addition & 1 deletion src/Services/InsertInArchiveService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function insertInArchive(array $changes, int $batchSize = 5000): void
// $value = $this->escapeQuotationMarks($value);
// }

$change['archived_at'] = $date;
$change[$entityChange->getArchivedAtFieldName()] = $date;
$this->executeQuery($tableName, $columnNames, $change);

$counter++;
Expand Down
2 changes: 1 addition & 1 deletion src/Services/MoveBetweenTablesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function moveToArchive(array $changes, int $batchSize = 1000): void
$value = $this->escapeQuotationMarks($value);
}

$change['archived_at'] = $date;
$change[$entityChange->getArchivedAtFieldName()] = $date;
}

$this->executeQuery($tableName, $archiveName, $columnNames, $changes, $batchSize);
Expand Down

0 comments on commit 5c2ec12

Please sign in to comment.