possibly fixes the issue with post number calculation

This commit is contained in:
Daniël Klabbers 2022-03-23 21:30:46 +01:00 committed by Daniël Klabbers
parent 105481a181
commit e429558d0e

View File

@ -16,7 +16,9 @@ use Flarum\Foundation\EventGeneratorTrait;
use Flarum\Notification\Notification; use Flarum\Notification\Notification;
use Flarum\Post\Event\Deleted; use Flarum\Post\Event\Deleted;
use Flarum\User\User; use Flarum\User\User;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Expression;
/** /**
* @property int $id * @property int $id
@ -91,7 +93,19 @@ class Post extends AbstractModel
// discussion. // discussion.
static::creating(function (self $post) { static::creating(function (self $post) {
$post->type = $post::$type; $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(); $post->discussion->save();
}); });