fix: first posts having NULL as value for number column (#3414)

* fix: first posts having `NULL` as value for `number` column
* chore: add note about raw db function `IFNULL`
This commit is contained in:
Sami Mazouz 2022-05-08 23:21:11 +01:00 committed by GitHub
parent 2e89ba3c94
commit 1954d0383e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
node_modules node_modules
vendor
composer.lock

View File

@ -96,12 +96,13 @@ class Post extends AbstractModel
/** @var ConnectionInterface $db */ /** @var ConnectionInterface $db */
$db = static::getConnectionResolver(); $db = static::getConnectionResolver();
$post->number = new Expression('('.$db $post->number = new Expression('('.
->table('posts', 'pn') $db->table('posts', 'pn')
->whereRaw($db->getTablePrefix().'pn.discussion_id = '.intval($post->discussion_id)) ->whereRaw($db->getTablePrefix().'pn.discussion_id = '.intval($post->discussion_id))
->select($db->raw('max('.$db->getTablePrefix().'pn.number) + 1')) // IFNULL only works on MySQL/MariaDB
->selectRaw('IFNULL(MAX('.$db->getTablePrefix().'pn.number), 0) + 1')
->toSql() ->toSql()
.')'); .')');
}); });
static::created(function (self $post) { static::created(function (self $post) {