Skip to content

Commit f48a569

Browse files
authored
Ability to disable cascading for trash (#69)
1 parent 79ac785 commit f48a569

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ item, you can just attach the behavior to the related table classes, and set the
7373
This works on relationships where the item being deleted in the owning side of
7474
the relationship. Which means that the related table should contain the foreign key.
7575

76+
If you don't want to cascade on trash:
77+
```php
78+
// in the initialize() method
79+
$this->addBehavior('Muffin/Trash.Trash', [
80+
'cascadeOnTrash' => false,
81+
]);
82+
```
83+
7684
### Custom Finders
7785

7886
- **onlyTrashed** - helps getting only those trashed records.

src/Model/Behavior/TrashBehavior.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TrashBehavior extends Behavior
4343
'Model.beforeDelete',
4444
'Model.beforeFind',
4545
],
46+
'cascadeOnTrash' => true,
4647
];
4748

4849
/**
@@ -145,10 +146,12 @@ public function trash(EntityInterface $entity, array $options = []): bool
145146
}
146147
}
147148

148-
$associations = $this->_table->associations()->getByType(['HasOne', 'HasMany']);
149-
foreach ($associations as $association) {
150-
if ($this->_isRecursable($association, $this->_table)) {
151-
$association->cascadeDelete($entity, ['_primary' => false] + $options);
149+
if ($this->getConfig('cascadeOnTrash')) {
150+
$associations = $this->_table->associations()->getByType(['HasOne', 'HasMany']);
151+
foreach ($associations as $association) {
152+
if ($this->_isRecursable($association, $this->_table)) {
153+
$association->cascadeDelete($entity, ['_primary' => false] + $options);
154+
}
152155
}
153156
}
154157

tests/TestCase/Model/Behavior/TrashBehaviorTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,38 @@ public function testCascadingTrash()
526526
$this->assertInstanceOf(DateTime::class, $article->comments[0]->trashed);
527527
}
528528

529+
/**
530+
* When cascadeTrashAndRestore = false
531+
* Ensure that when trashing it will not cascade into related dependent records
532+
*
533+
* @return void
534+
*/
535+
public function testDisabledCascadingForTrash()
536+
{
537+
$association = $this->Articles->Comments;
538+
$association->setDependent(true);
539+
$association->setCascadeCallbacks(true);
540+
541+
// disable cascade trash/restore
542+
$this->Articles->behaviors()->get('Trash')->setConfig('cascadeOnTrash', false);
543+
544+
$article = $this->Articles->get(1);
545+
$this->Articles->trash($article);
546+
547+
$article = $this->Articles->find('withTrashed')
548+
->where(['Articles.id' => 1])
549+
->contain(['Comments' => [
550+
'finder' => 'withTrashed',
551+
]])
552+
->first();
553+
554+
$this->assertNotEmpty($article->trashed);
555+
$this->assertInstanceOf(DateTime::class, $article->trashed);
556+
557+
// expect not trashed
558+
$this->assertEmpty($article->comments[0]->trashed);
559+
}
560+
529561
public function testCascadingUntrashOptionsArePassedToSave()
530562
{
531563
$association = $this->Articles->Comments;

0 commit comments

Comments
 (0)