Skip to content

Commit

Permalink
Merge branch 'marksalmon/main'
Browse files Browse the repository at this point in the history
Closes #75
  • Loading branch information
michaeldyrynda committed Jan 6, 2025
2 parents d5d78b9 + b5d9bc9 commit aefa413
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/CascadeSoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ protected static function bootCascadeSoftDeletes()

$model->runCascadingDeletes();
});

if (method_exists(static::class, 'restoring')) {
static::restoring(function ($model) {
$model->validateCascadingSoftDelete();

$model->runCascadingRestores();
});
}
}


Expand Down Expand Up @@ -85,6 +93,37 @@ private function handleRecords($relationship, $cb)
}
}

/**
* Run the cascading restore for this model.
*
* @return void
*/
protected function runCascadingRestores()
{
foreach ($this->getCascadingDeletes() as $relationship) {
$this->cascadeRestores($relationship);
}
}

/**
* Cascade restore the given relationship on the given mode.
*
* @param string $relationship
* @return return
*/
protected function cascadeRestores($relationship)
{
if (method_exists($this->{$relationship}()->getModel(), 'runSoftDelete')) {
foreach ($this->{$relationship}()
->withTrashed()
->where($this->{$relationship}()->getModel()->getDeletedAtColumn(), '>=', $this->{$this->getDeletedAtColumn()})
->get() as $model
) {
$model->restore();
}
}
}


/**
* Determine if the current model implements soft deletes.
Expand Down
48 changes: 48 additions & 0 deletions tests/CascadeSoftDeletesIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Carbon\Carbon;
use Dyrynda\Database\Support\CascadeSoftDeleteException;
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager;
Expand Down Expand Up @@ -47,6 +48,7 @@
$table->integer('post_id')->unsigned();
$table->string('body');
$table->timestamps();
$table->softDeletes();
});

$manager->schema()->create('post_types', function ($table) {
Expand Down Expand Up @@ -236,6 +238,52 @@
expect(PostType::where('id', $type->id)->get())->toHaveCount(0);
});

it('cascades restore of soft deleted children', function () {
$post = Post::create([
'title' => 'How to cascade restores in Laravel',
'body' => 'This is how you cascade restores in Laravel',
]);

attachCommentsToPost($post);

expect($post->comments()->count())->toBe(3);

$post->delete();

expect(Comment::where('post_id', $post->id)->count())->toBe(0);

$post->restore();

expect($post->comments()->count())->toBe(3);
});

it('does not cascade restore of children deleted before the parent', function () {
$post = Post::create([
'title' => 'How to cascade restores in Laravel',
'body' => 'This is how you cascade restores in Laravel',
]);

attachCommentsToPost($post);

expect($post->comments()->count())->toBe(3);

Carbon::setTestNow(Carbon::now()->subSeconds(1));

$post->comments()->take(1)->delete();

expect($post->comments()->count())->toBe(2);

Carbon::setTestNow();

$post->delete();

expect(Comment::where('post_id', $post->id)->count())->toBe(0);

$post->restore();

expect($post->comments()->count())->toBe(2);
});

/**
* Attach some post types to the given author.
*
Expand Down
3 changes: 3 additions & 0 deletions tests/Entities/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Tests\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Comment extends Model
{
use SoftDeletes;

protected $fillable = ['body'];

public function post()
Expand Down

0 comments on commit aefa413

Please sign in to comment.