[DB] Add 'dql' method to wrap 'createQuery' and replace 'Gsactor' with 'GSActor'

This commit is contained in:
Hugo Sales 2020-08-14 22:36:08 +00:00 committed by Hugo Sales
parent 688ee18411
commit 8cfa883c1b
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -34,6 +34,7 @@ use App\Util\Formatting;
use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\ExpressionBuilder; use Doctrine\Common\Collections\ExpressionBuilder;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Exception; use Exception;
abstract class DB abstract class DB
@ -44,6 +45,16 @@ abstract class DB
self::$em = $m; self::$em = $m;
} }
public static function dql(string $query, ?array $params)
{
$q = new Query(self::$em);
$q->setDQL($query);
foreach ($params as $k => $v) {
$q->setParameter($k, $v);
}
return $q->getResult();
}
private static array $find_by_ops = ['or', 'and', 'eq', 'neq', 'lt', 'lte', private static array $find_by_ops = ['or', 'and', 'eq', 'neq', 'lt', 'lte',
'gt', 'gte', 'is_null', 'in', 'not_in', 'gt', 'gte', 'is_null', 'in', 'not_in',
'contains', 'member_of', 'starts_with', 'ends_with', ]; 'contains', 'member_of', 'starts_with', 'ends_with', ];
@ -100,8 +111,9 @@ abstract class DB
$args[0] = '\App\Entity\\' . ucfirst(Formatting::snakeCaseToCamelCase($args[0])); $args[0] = '\App\Entity\\' . ucfirst(Formatting::snakeCaseToCamelCase($args[0]));
} }
} }
if (($args[0] ?? '') === '\App\Entity\Gsactor') {
$args[0] = '\App\Entity\GSActor'; if (isset($args[0]) && is_string($args[0])) {
$args[0] = preg_replace('/Gsactor/', 'GSActor', $args[0] ?? '');
} }
return self::$em->{$name}(...$args); return self::$em->{$name}(...$args);