From 7c35fde8bc4fba4f9956815d44a3b52ce94112c8 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sat, 25 Jul 2020 01:55:39 +0000 Subject: [PATCH] [FIX] Fix bug in DATABASE.php, since findBy can return different types --- src/Core/DB/DB.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Core/DB/DB.php b/src/Core/DB/DB.php index 52bdfe6e05..53cd1e6287 100644 --- a/src/Core/DB/DB.php +++ b/src/Core/DB/DB.php @@ -34,6 +34,7 @@ use App\Util\Formatting; use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\ExpressionBuilder; use Doctrine\ORM\EntityManagerInterface; +use Exception; abstract class DB { @@ -44,8 +45,8 @@ abstract class DB } private static array $find_by_ops = ['or', 'and', 'eq', 'neq', 'lt', 'lte', - 'gt', 'gte', 'is_null', 'in', 'not_in', - 'contains', 'member_of', 'starts_with', 'ends_with', ]; + 'gt', 'gte', 'is_null', 'in', 'not_in', + 'contains', 'member_of', 'starts_with', 'ends_with', ]; private static function buildExpression(ExpressionBuilder $eb, array $criteria) { @@ -84,7 +85,16 @@ abstract class DB public static function findOneBy(string $table, array $criteria, ?array $orderBy = null, ?int $offset = null) { - return self::findBy($table, $criteria, $orderBy, 1, $offset)->first(); + $res = self::findBy($table, $criteria, $orderBy, 1, $offset); + if (is_array($res)) { + if (count($res)) { + return $res[0]; + } else { + throw new Exception("No value in table {$table} matches the requested criteria"); + } + } else { + return $res->first(); + } } public static function __callStatic(string $name, array $args)