Skip to content

Commit 9158472

Browse files
Merge pull request #12 from michaeldyrynda/fix/#10-cascade-has-one-relationship
Add handling for hasOne relationships
2 parents 28bc74a + 16a33b4 commit 9158472

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
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+
protected 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: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?php
22

3-
use Illuminate\Database\Capsule\Manager;
4-
use Illuminate\Events\Dispatcher;
53
use Illuminate\Container\Container;
4+
use Illuminate\Database\Capsule\Manager;
65
use Illuminate\Database\Eloquent\Model;
76
use Illuminate\Database\Eloquent\SoftDeletes;
7+
use Illuminate\Events\Dispatcher;
88

99
class CascadeSoftDeletesIntegrationTest extends PHPUnit_Framework_TestCase
1010
{
1111
public static function setupBeforeClass()
1212
{
1313
$manager = new Manager();
1414
$manager->addConnection([
15-
'driver' => 'sqlite',
15+
'driver' => 'sqlite',
1616
'database' => ':memory:',
1717
]);
1818

@@ -43,6 +43,13 @@ public static function setupBeforeClass()
4343
$table->string('body');
4444
$table->timestamps();
4545
});
46+
47+
$manager->schema()->create('post_types', function ($table) {
48+
$table->increments('id');
49+
$table->integer('post_id')->unsigned()->nullable();
50+
$table->string('label');
51+
$table->timestamps();
52+
});
4653
}
4754

4855

@@ -201,6 +208,22 @@ public function it_handles_grandchildren()
201208
}
202209
}
203210

211+
/** @test */
212+
public function it_cascades_a_has_one_relationship()
213+
{
214+
$post = Tests\Entities\Post::create([
215+
'title' => 'Cascade a has one relationship',
216+
'body' => 'This is how you cascade a has one relationship',
217+
]);
218+
219+
$type = new Tests\Entities\PostType(['label' => 'Test']);
220+
221+
$post->postType()->save($type);
222+
223+
$post->delete();
224+
$this->assertCount(0, Tests\Entities\PostType::where('id', $type->id)->get());
225+
}
226+
204227
/**
205228
* Attach some dummy posts (w/ comments) to the given author.
206229
*
@@ -241,5 +264,4 @@ private function attachCommentsToPost($post)
241264

242265
return $post;
243266
}
244-
245267
}

tests/Entities/Post.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ class Post extends Model
1212

1313
public $dates = ['deleted_at'];
1414

15-
protected $cascadeDeletes = ['comments'];
15+
protected $cascadeDeletes = ['comments', 'postType'];
1616

1717
protected $fillable = ['title', 'body'];
1818

1919
public function comments()
2020
{
2121
return $this->hasMany('Tests\Entities\Comment');
2222
}
23+
24+
public function postType()
25+
{
26+
return $this->hasOne('Tests\Entities\PostType', 'post_id');
27+
}
2328
}

tests/Entities/PostType.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Tests\Entities;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class PostType extends Model
8+
{
9+
protected $fillable = ['label'];
10+
11+
public function post()
12+
{
13+
return $this->belongsTo('Test\Entities\Post');
14+
}
15+
}

0 commit comments

Comments
 (0)