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

Add an option to allow users to detect "soft deletes" #70

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ or use the purge option:
$table->delete($entity, ['purge' => true]);
```

## Detecting trashing
If you need to distinguish between deletion and trashing the behavior
adds the ['muffin.trash.soft-delete' => true ] option to the afterDelete event
it creates when trashing.

### Cascading deletion

If you'd like to have related records marked as trashed when deleting a parent
Expand Down
4 changes: 4 additions & 0 deletions src/Model/Behavior/TrashBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@

namespace Muffin\Trash\Model\Behavior;

use ArrayObject;

Check failure on line 6 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Type ArrayObject is not used in this file.
use Cake\Core\Configure;
use Cake\Core\Exception\CakeException;
use Cake\Database\Expression\BetweenExpression;

Check failure on line 9 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Type Cake\Database\Expression\BetweenExpression is not used in this file.
use Cake\Database\Expression\ComparisonExpression;

Check failure on line 10 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Type Cake\Database\Expression\ComparisonExpression is not used in this file.
use Cake\Database\Expression\IdentifierExpression;

Check failure on line 11 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Type Cake\Database\Expression\IdentifierExpression is not used in this file.
use Cake\Database\Expression\QueryExpression;

Check failure on line 12 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Type Cake\Database\Expression\QueryExpression is not used in this file.
use Cake\Database\Expression\UnaryExpression;

Check failure on line 13 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Type Cake\Database\Expression\UnaryExpression is not used in this file.
use Cake\Database\Query\SelectQuery;

Check failure on line 14 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Type Cake\Database\Query\SelectQuery is not used in this file.
use Cake\Datasource\EntityInterface;

Check failure on line 15 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Type Cake\Datasource\EntityInterface is not used in this file.
use Cake\Event\EventInterface;

Check failure on line 16 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Type Cake\Event\EventInterface is not used in this file.
use Cake\I18n\DateTime;

Check failure on line 17 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Type Cake\I18n\DateTime is not used in this file.
use Cake\ORM\Association;
use Cake\ORM\Behavior;
use Cake\ORM\Table;
Expand All @@ -27,6 +27,8 @@
*/
class TrashBehavior extends Behavior
{
public const DELETE_OPTION_NAME = 'trash;
nicosp marked this conversation as resolved.
Show resolved Hide resolved

/**
* Default configuration.
*
Expand All @@ -37,7 +39,7 @@
* @var array<string, mixed>
*/
protected array $_defaultConfig = [
'field' => null,

Check failure on line 42 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Syntax error, unexpected T_STRING, expecting ';' on line 42

Check failure on line 42 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

ParseError

src/Model/Behavior/TrashBehavior.php:42:10: ParseError: Syntax error, unexpected T_STRING, expecting ';' on line 42 (see https://psalm.dev/173)
'priority' => null,
'events' => [
'Model.beforeDelete',
Expand Down Expand Up @@ -118,6 +120,8 @@
return;
}

$options[self::SOFT_DELETE_OPTION] = true;

/** @var \Cake\ORM\Table $table */
$table = $event->getSubject();
$table->dispatchEvent('Model.afterDelete', [
Expand Down Expand Up @@ -343,8 +347,8 @@
/**
* Returns the table's field used to mark a `trashed` row.
*
* @param bool $aliased Should field be aliased or not. Default true.

Check warning on line 350 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Silencing errors is discouraged; found: @param bool...
* @return string

Check warning on line 351 in src/Model/Behavior/TrashBehavior.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Silencing errors is discouraged; found: @return string...
*/
public function getTrashField(bool $aliased = true): string
{
Expand Down
Loading