From 1954d0383e0efb943b3b0a97f93aa1976d73efa6 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sun, 8 May 2022 23:21:11 +0100 Subject: [PATCH] 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` --- .gitignore | 4 +++- framework/core/src/Post/Post.php | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index b512c09d4..b1d62d565 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -node_modules \ No newline at end of file +node_modules +vendor +composer.lock diff --git a/framework/core/src/Post/Post.php b/framework/core/src/Post/Post.php index 8c72f6c53..8a0ba4ab5 100644 --- a/framework/core/src/Post/Post.php +++ b/framework/core/src/Post/Post.php @@ -96,12 +96,13 @@ class Post extends AbstractModel /** @var ConnectionInterface $db */ $db = static::getConnectionResolver(); - $post->number = new Expression('('.$db - ->table('posts', 'pn') + $post->number = new Expression('('. + $db->table('posts', 'pn') ->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() - .')'); + .')'); }); static::created(function (self $post) {