From 00bf235809d1f298cd2860745dde70cabb2ac141 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 8 May 2015 18:12:02 +0200 Subject: [PATCH] Fix discussion seeder not using the correct post subtypes. Related to #67. --- framework/core/src/Core/Models/Post.php | 14 +++++++++++++- .../src/Core/Seeders/DiscussionsTableSeeder.php | 11 +++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/framework/core/src/Core/Models/Post.php b/framework/core/src/Core/Models/Post.php index ba2f59ca2..f8353497b 100755 --- a/framework/core/src/Core/Models/Post.php +++ b/framework/core/src/Core/Models/Post.php @@ -3,7 +3,7 @@ use Tobscure\Permissible\Permissible; use Flarum\Core\Events\PostWasDeleted; -class Post extends Model +abstract class Post extends Model { use Permissible; @@ -46,6 +46,17 @@ class Post extends Model */ protected static $types = []; + /** + * The type of post this is, to be stored in the posts table. + * + * Should be overwritten by subclasses with the value that is + * to be stored in the database, which will then be used for + * mapping the hydrated model instance to the proper subtype. + * + * @var string + */ + public static $type = ''; + /** * Raise an event when a post is deleted. Add an event listener to set the * post's number, and update the discussion's number index, when inserting @@ -58,6 +69,7 @@ class Post extends Model parent::boot(); static::creating(function ($post) { + $post->type = $post::$type; $post->number = ++$post->discussion->number_index; $post->discussion->save(); }); diff --git a/framework/core/src/Core/Seeders/DiscussionsTableSeeder.php b/framework/core/src/Core/Seeders/DiscussionsTableSeeder.php index 9dbd3c806..2537dd530 100644 --- a/framework/core/src/Core/Seeders/DiscussionsTableSeeder.php +++ b/framework/core/src/Core/Seeders/DiscussionsTableSeeder.php @@ -2,7 +2,9 @@ use Illuminate\Database\Seeder; use DB; +use Flarum\Core\Models\CommentPost; use Flarum\Core\Models\Discussion; +use Flarum\Core\Models\DiscussionRenamedPost; use Flarum\Core\Models\Post; use Flarum\Core\Models\User; use Flarum\Core\Models\DiscussionState; @@ -33,12 +35,11 @@ class DiscussionsTableSeeder extends Seeder ]); $discussion->comments_count = $posts_count; - $post = Post::create([ + $post = CommentPost::create([ 'discussion_id' => $discussion->id, 'number' => 1, 'time' => $discussion->start_time, 'user_id' => $discussion->start_user_id, - 'type' => 'comment', 'content' => $faker->realText(rand(100, 1000)) ]); @@ -60,11 +61,10 @@ class DiscussionsTableSeeder extends Seeder if (rand(1, 100) == 1) { $discussion->comments_count--; - $post = Post::create([ + $post = DiscussionRenamedPost::create([ 'discussion_id' => $discussion->id, 'time' => $startTime = date_add($startTime, date_interval_create_from_date_string('1 second')), 'user_id' => rand(1, $users), - 'type' => 'discussionRenamed', 'content' => json_encode(array($faker->realText(rand(20, 40)), $discussion->title)) ]); } else { @@ -75,12 +75,11 @@ class DiscussionsTableSeeder extends Seeder $discussion->comments_count--; } - $post = Post::create([ + $post = CommentPost::create([ 'discussion_id' => $discussion->id, 'number' => $j + 2 + $numberOffset, 'time' => $startTime = date_add($startTime, date_interval_create_from_date_string('1 second')), 'user_id' => rand(1, $users), - 'type' => 'comment', 'content' => $faker->realText(rand(50, 500)), 'edit_time' => $edited ? $startTime = date_add($startTime, date_interval_create_from_date_string('1 second')) : null, 'edit_user_id' => $edited ? rand(1, $users) : null,