Skip to content

Commit

Permalink
Add an option to allow users to detect "soft deletes" (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosp authored Jan 14, 2025
1 parent f1ceea7 commit 9d3ff34
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
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 ['trash' => 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 @@ -27,6 +27,8 @@
*/
class TrashBehavior extends Behavior
{
public const DELETE_OPTION_NAME = 'trash';

/**
* Default configuration.
*
Expand Down Expand Up @@ -118,6 +120,8 @@ public function beforeDelete(EventInterface $event, EntityInterface $entity, Arr
return;
}

$options[self::DELETE_OPTION_NAME] = true;

/** @var \Cake\ORM\Table $table */
$table = $event->getSubject();
$table->dispatchEvent('Model.afterDelete', [
Expand Down

0 comments on commit 9d3ff34

Please # to comment.