Skip to content

Commit

Permalink
Ability to disable cascading for trash (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
kolorafa authored Aug 29, 2024
1 parent 79ac785 commit f48a569
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ item, you can just attach the behavior to the related table classes, and set the
This works on relationships where the item being deleted in the owning side of
the relationship. Which means that the related table should contain the foreign key.

If you don't want to cascade on trash:
```php
// in the initialize() method
$this->addBehavior('Muffin/Trash.Trash', [
'cascadeOnTrash' => false,
]);
```

### Custom Finders

- **onlyTrashed** - helps getting only those trashed records.
Expand Down
11 changes: 7 additions & 4 deletions src/Model/Behavior/TrashBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class TrashBehavior extends Behavior
'Model.beforeDelete',
'Model.beforeFind',
],
'cascadeOnTrash' => true,
];

/**
Expand Down Expand Up @@ -145,10 +146,12 @@ public function trash(EntityInterface $entity, array $options = []): bool
}
}

$associations = $this->_table->associations()->getByType(['HasOne', 'HasMany']);
foreach ($associations as $association) {
if ($this->_isRecursable($association, $this->_table)) {
$association->cascadeDelete($entity, ['_primary' => false] + $options);
if ($this->getConfig('cascadeOnTrash')) {
$associations = $this->_table->associations()->getByType(['HasOne', 'HasMany']);
foreach ($associations as $association) {
if ($this->_isRecursable($association, $this->_table)) {
$association->cascadeDelete($entity, ['_primary' => false] + $options);
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/TestCase/Model/Behavior/TrashBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,38 @@ public function testCascadingTrash()
$this->assertInstanceOf(DateTime::class, $article->comments[0]->trashed);
}

/**
* When cascadeTrashAndRestore = false
* Ensure that when trashing it will not cascade into related dependent records
*
* @return void
*/
public function testDisabledCascadingForTrash()
{
$association = $this->Articles->Comments;
$association->setDependent(true);
$association->setCascadeCallbacks(true);

// disable cascade trash/restore
$this->Articles->behaviors()->get('Trash')->setConfig('cascadeOnTrash', false);

$article = $this->Articles->get(1);
$this->Articles->trash($article);

$article = $this->Articles->find('withTrashed')
->where(['Articles.id' => 1])
->contain(['Comments' => [
'finder' => 'withTrashed',
]])
->first();

$this->assertNotEmpty($article->trashed);
$this->assertInstanceOf(DateTime::class, $article->trashed);

// expect not trashed
$this->assertEmpty($article->comments[0]->trashed);
}

public function testCascadingUntrashOptionsArePassedToSave()
{
$association = $this->Articles->Comments;
Expand Down

0 comments on commit f48a569

Please # to comment.