From e8cd2d4111ce9b561485d8052a310f3bb5ddede8 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 21 Jul 2018 17:18:40 +0930 Subject: [PATCH] Fix entity deletion Foreign keys take care of most of this for us! --- ...55300_change_discussions_add_foreign_keys.php | 4 ++-- framework/core/src/Discussion/Discussion.php | 15 --------------- framework/core/src/Group/Group.php | 2 -- framework/core/src/User/User.php | 16 ---------------- 4 files changed, 2 insertions(+), 35 deletions(-) diff --git a/framework/core/migrations/2018_01_11_155300_change_discussions_add_foreign_keys.php b/framework/core/migrations/2018_01_11_155300_change_discussions_add_foreign_keys.php index 6cf2adc44..3e33b765c 100644 --- a/framework/core/migrations/2018_01_11_155300_change_discussions_add_foreign_keys.php +++ b/framework/core/migrations/2018_01_11_155300_change_discussions_add_foreign_keys.php @@ -37,8 +37,8 @@ return [ $table->foreign('user_id')->references('id')->on('users')->onDelete('set null'); $table->foreign('last_posted_user_id')->references('id')->on('users')->onDelete('set null'); $table->foreign('hidden_user_id')->references('id')->on('users')->onDelete('set null'); - $table->foreign('first_post_id')->references('id')->on('posts'); - $table->foreign('last_post_id')->references('id')->on('posts'); + $table->foreign('first_post_id')->references('id')->on('posts')->onDelete('set null'); + $table->foreign('last_post_id')->references('id')->on('posts')->onDelete('set null'); }); }, diff --git a/framework/core/src/Discussion/Discussion.php b/framework/core/src/Discussion/Discussion.php index cfad12d27..d9feb4b6c 100644 --- a/framework/core/src/Discussion/Discussion.php +++ b/framework/core/src/Discussion/Discussion.php @@ -100,21 +100,6 @@ class Discussion extends AbstractModel static::deleted(function (Discussion $discussion) { $discussion->raise(new Deleted($discussion)); - - // Delete all of the posts in the discussion. Before we delete them - // in a big batch query, we will loop through them and raise a - // PostWasDeleted event for each post. - $posts = $discussion->posts()->allTypes(); - - foreach ($posts->cursor() as $post) { - $discussion->raise(new PostDeleted($post)); - } - - $posts->delete(); - - // Delete all of the 'state' records for all of the users who have - // read the discussion. - $discussion->readers()->detach(); }); static::saving(function (Discussion $discussion) { diff --git a/framework/core/src/Group/Group.php b/framework/core/src/Group/Group.php index 19f1b19fd..103dd4fea 100644 --- a/framework/core/src/Group/Group.php +++ b/framework/core/src/Group/Group.php @@ -64,8 +64,6 @@ class Group extends AbstractModel static::deleted(function (Group $group) { $group->raise(new Deleted($group)); - - $group->permissions()->delete(); }); } diff --git a/framework/core/src/User/User.php b/framework/core/src/User/User.php index aad9c5f0b..4ad6e1476 100644 --- a/framework/core/src/User/User.php +++ b/framework/core/src/User/User.php @@ -126,22 +126,6 @@ class User extends AbstractModel static::deleted(function (User $user) { $user->raise(new Deleted($user)); - - // Delete all of the posts by the user. Before we delete them - // in a big batch query, we will loop through them and raise a - // PostWasDeleted event for each post. - $posts = $user->posts()->allTypes(); - - foreach ($posts->cursor() as $post) { - $user->raise(new PostDeleted($post)); - } - - $posts->delete(); - - $user->read()->detach(); - $user->groups()->detach(); - $user->accessTokens()->delete(); - $user->notifications()->delete(); }); static::$dispatcher->fire(