From 046c6e216860da0276a8268c18a739edc8fe6df3 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 25 Jun 2015 09:48:28 +0930 Subject: [PATCH] Update start_post_id when starting a discussion --- .../StartDiscussionCommandHandler.php | 3 +++ framework/core/src/Core/Models/Discussion.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/framework/core/src/Core/Handlers/Commands/StartDiscussionCommandHandler.php b/framework/core/src/Core/Handlers/Commands/StartDiscussionCommandHandler.php index 4a9b61906..5a35ed839 100644 --- a/framework/core/src/Core/Handlers/Commands/StartDiscussionCommandHandler.php +++ b/framework/core/src/Core/Handlers/Commands/StartDiscussionCommandHandler.php @@ -44,9 +44,12 @@ class StartDiscussionCommandHandler // attributes as posting the reply will have changed some of them (e.g. // last_time.) $discussion->setRawAttributes($post->discussion->getAttributes(), true); + $discussion->setStartPost($post); $this->dispatchEventsFor($discussion); + $discussion->save(); + return $post->discussion; } } diff --git a/framework/core/src/Core/Models/Discussion.php b/framework/core/src/Core/Models/Discussion.php index fc338ef98..1655c5cd4 100755 --- a/framework/core/src/Core/Models/Discussion.php +++ b/framework/core/src/Core/Models/Discussion.php @@ -134,6 +134,21 @@ class Discussion extends Model return $this; } + /** + * Set the discussion's start post details. + * + * @param \Flarum\Core\Models\Post $post + * @return $this + */ + public function setStartPost(Post $post) + { + $this->start_time = $post->time; + $this->start_user_id = $post->user_id; + $this->start_post_id = $post->id; + + return $this; + } + /** * Set the discussion's last post details. * @@ -350,6 +365,10 @@ class Discussion extends Model */ public function stateFor(User $user) { + // If the state is already eager-loaded, we'll return that. + // Unfortunately we can't check to see if the user ID is the same, + // because the user may not have a state entry, in which case the loaded + // relation will be null. if ($this->isRelationLoaded('state')) { return $this->relations['state']; }