From e429558d0ef5c1d4c1879953f03eed0380df6b8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Klabbers?= Date: Wed, 23 Mar 2022 21:30:46 +0100 Subject: [PATCH] possibly fixes the issue with post number calculation --- framework/core/src/Post/Post.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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(); });