forked from GNUsocial/gnu-social
[DATABASE] Refactor DB.php and make findBy always return an array, instead of a doctrine collection
This commit is contained in:
parent
51f65edb55
commit
d6a7843240
@ -70,31 +70,27 @@ 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)) {
|
||||
if (count($res) == 1) {
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user