From d6a7843240b11342e36a7deacf13a6301fdf0824 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sat, 25 Jul 2020 17:49:57 +0000 Subject: [PATCH] [DATABASE] Refactor DB.php and make findBy always return an array, instead of a doctrine collection --- src/Core/DB/DB.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Core/DB/DB.php b/src/Core/DB/DB.php index 53cd1e6287..6c96d1ecc4 100644 --- a/src/Core/DB/DB.php +++ b/src/Core/DB/DB.php @@ -70,30 +70,26 @@ abstract class DB return $expressions; } - public static function findBy(string $table, array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null) + public static function findBy(string $table, array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array { $criteria = array_change_key_case($criteria); $ops = array_intersect(array_keys($criteria), self::$find_by_ops); - $repo = self::__callStatic('getRepository', [$table]); + $repo = self::getRepository($table); if (empty($ops)) { return $repo->findBy($criteria, $orderBy, $limit, $offset); } else { $criteria = new Criteria(self::buildExpression(Criteria::expr(), $criteria), $orderBy, $offset, $limit); - return $repo->matching($criteria); + return $repo->matching($criteria)->toArray(); // Always work with array or it becomes really complicated } } public static function findOneBy(string $table, array $criteria, ?array $orderBy = null, ?int $offset = null) { $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"); - } + if (count($res) == 1) { + return $res[0]; } else { - return $res->first(); + throw new Exception("No value in table {$table} matches the requested criteria"); } }