Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SoftDeleteable] Make deleted value configurable for SoftDeleteableFilter.php #2873

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
6 changes: 5 additions & 1 deletion src/Mapping/Annotation/SoftDeleteable.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ final class SoftDeleteable implements GedmoAnnotation

public bool $hardDelete = true;

public ?string $nonDeletedColumnValue = null;

/**
* @param array<string, mixed> $data
*/
public function __construct(array $data = [], string $fieldName = 'deletedAt', bool $timeAware = false, bool $hardDelete = true)
public function __construct(array $data = [], string $fieldName = 'deletedAt', bool $timeAware = false, bool $hardDelete = true, ?string $nonDeletedColumnValue = null)
{
if ([] !== $data) {
Deprecation::trigger(
Expand All @@ -53,12 +55,14 @@ public function __construct(array $data = [], string $fieldName = 'deletedAt', b
$this->fieldName = $this->getAttributeValue($data, 'fieldName', $args, 1, $fieldName);
$this->timeAware = $this->getAttributeValue($data, 'timeAware', $args, 2, $timeAware);
$this->hardDelete = $this->getAttributeValue($data, 'hardDelete', $args, 3, $hardDelete);
$this->nonDeletedColumnValue = $this->getAttributeValue($data, 'nonDeletedColumnValue', $args, 4, $nonDeletedColumnValue);

return;
}

$this->fieldName = $fieldName;
$this->timeAware = $timeAware;
$this->hardDelete = $hardDelete;
$this->nonDeletedColumnValue = $nonDeletedColumnValue;
}
}
2 changes: 1 addition & 1 deletion src/SoftDeleteable/Filter/SoftDeleteableFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAli

$column = $quoteStrategy->getColumnName($config['fieldName'], $targetEntity, $platform);

$addCondSql = $targetTableAlias.'.'.$column.' IS NULL';
$addCondSql = $targetTableAlias.'.'.$column.' '.(isset($config['nonDeletedColumnValue']) ? '= '.$config['nonDeletedColumnValue'] : 'IS NULL');
if (isset($config['timeAware']) && $config['timeAware']) {
$addCondSql = "({$addCondSql} OR {$targetTableAlias}.{$column} > {$platform->getCurrentTimestampSQL()})";
}
Expand Down
8 changes: 8 additions & 0 deletions src/SoftDeleteable/Mapping/Driver/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public function readExtendedMetadata($meta, array &$config)

$config['hardDelete'] = $annot->hardDelete;
}

if (isset($annot->nonDeletedColumnValue)) {
if (!is_string($annot->nonDeletedColumnValue)) {
throw new InvalidMappingException('nonDeletedColumnValue must be string. '.gettype($annot->nonDeletedColumnValue).' provided.');
}

$config['nonDeletedColumnValue'] = $annot->nonDeletedColumnValue;
}
}

$this->validateFullMetadata($meta, $config);
Expand Down
5 changes: 5 additions & 0 deletions src/SoftDeleteable/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public function readExtendedMetadata($meta, array &$config)
if ($this->_isAttributeSet($xml->{'soft-deleteable'}, 'hard-delete')) {
$config['hardDelete'] = $this->_getBooleanAttribute($xml->{'soft-deleteable'}, 'hard-delete');
}

$config['nonDeletedColumnValue'] = null;
if ($this->_isAttributeSet($xml->{'soft-deleteable'}, 'deleted-value')) {
$config['nonDeletedColumnValue'] = $this->_getAttribute($xml->{'soft-deleteable'}, 'non-deleted-column-value');
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/SoftDeleteable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public function readExtendedMetadata($meta, array &$config)
}
$config['hardDelete'] = $classMapping['soft_deleteable']['hard_delete'];
}

$config['nonDeletedColumnValue'] = null;
if (isset($classMapping['soft_deleteable']['deleted_value'])) {
if (!is_string($classMapping['soft_deleteable']['deleted_value'])) {
throw new InvalidMappingException('nonDeletedColumnValue must be string. '.gettype($classMapping['soft_deleteable']['deleted_value']).' provided.');
}
$config['nonDeletedColumnValue'] = $classMapping['soft_deleteable']['non_deleted_column_value'];
}
}
}

Expand Down