Skip to content

Commit 1cc777a

Browse files
Add tests, enhanced functionality to account for hasOne relationships
As a `hasOne` returns the instance of the model and not a collection, handle calls to delete it correctly. Ensure that we only attempt to delete models if a call to the relationship doesn't return null. Closes #10, closes #11
1 parent 9e7fc3d commit 1cc777a

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/CascadeSoftDeletes.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Iatstuti\Database\Support;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Database\Eloquent\Relations\Relation;
67
use LogicException;
78

@@ -35,9 +36,13 @@ protected static function bootCascadeSoftDeletes()
3536

3637
$delete = $model->forceDeleting ? 'forceDelete' : 'delete';
3738

38-
foreach ($model->getCascadingDeletes() as $relationship) {
39-
foreach ($model->{$relationship} as $child) {
40-
$child->{$delete}();
39+
foreach ($model->getActiveCascadingDeletes() as $relationship) {
40+
if ($model->{$relationship} instanceof Model) {
41+
$model->{$relationship}->{$delete}();
42+
} else {
43+
foreach ($model->{$relationship} as $child) {
44+
$child->{$delete}();
45+
}
4146
}
4247
}
4348
});
@@ -80,4 +85,17 @@ protected function getCascadingDeletes()
8085
{
8186
return isset($this->cascadeDeletes) ? (array) $this->cascadeDeletes : [];
8287
}
88+
89+
90+
/**
91+
* For the cascading deletes defined on the model, return only those that are not null.
92+
*
93+
* @return array
94+
*/
95+
private function getActiveCascadingDeletes()
96+
{
97+
return array_filter($this->getCascadingDeletes(), function ($relationship) {
98+
return ! is_null($this->{$relationship});
99+
});
100+
}
83101
}

tests/CascadeSoftDeletesIntegrationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public function it_handles_grandchildren()
212212
public function it_cascades_a_has_one_relationship()
213213
{
214214
$post = Tests\Entities\Post::create([
215-
'title' => 'Cascae a has one relationship',
215+
'title' => 'Cascade a has one relationship',
216216
'body' => 'This is how you cascade a has one relationship',
217217
]);
218218

@@ -221,7 +221,7 @@ public function it_cascades_a_has_one_relationship()
221221
$post->postType()->save($type);
222222

223223
$post->delete();
224-
$this->assertCount(0, Tests\Entities\Author::withTrashed()->where('id', $type->id)->get());
224+
$this->assertCount(0, Tests\Entities\PostType::where('id', $type->id)->get());
225225
}
226226

227227
/**

tests/Entities/Post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function comments()
2323

2424
public function postType()
2525
{
26-
return $this->hasOne('Tests\Entities\PostType');
26+
return $this->hasOne('Tests\Entities\PostType', 'post_id');
2727
}
2828
}

0 commit comments

Comments
 (0)