diff --git a/framework/core/src/Post/Post.php b/framework/core/src/Post/Post.php index f9deff3d2..daa4f5959 100644 --- a/framework/core/src/Post/Post.php +++ b/framework/core/src/Post/Post.php @@ -16,7 +16,9 @@ use Flarum\Foundation\EventGeneratorTrait; use Flarum\Notification\Notification; use Flarum\Post\Event\Deleted; use Flarum\User\User; +use Illuminate\Database\ConnectionInterface; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Query\Expression; /** * @property int $id @@ -91,7 +93,19 @@ class Post extends AbstractModel // discussion. static::creating(function (self $post) { $post->type = $post::$type; - $post->number = ++$post->discussion->post_number_index; + + /** @var ConnectionInterface $db */ + $db = static::getConnectionResolver(); + $post->number = new Expression('('. $db + ->table('posts', 'pn') + ->whereRaw('pn.discussion_id = ' . intval($post->discussion_id)) + ->select($db->raw('max(pn.number) + 1')) + ->toSql() + .')'); + }); + + static::created(function (self $post) { + $post->refresh(); $post->discussion->save(); });