[DB] Add wrapper for making native queries

This commit is contained in:
Hugo Sales 2020-09-04 18:45:28 +00:00 committed by Hugo Sales
parent a98af6ab6a
commit 80cefca90d
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -35,6 +35,7 @@ use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\ExpressionBuilder;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
use Exception;
abstract class DB
@ -55,6 +56,22 @@ abstract class DB
return $q->getResult();
}
public static function sql(string $query, array $entities, array $params = [])
{
$rsm = new ResultSetMappingBuilder(self::$em);
foreach ($entities as $alias => $entity) {
$rsm->addRootEntityFromClassMetadata($entity, $alias);
}
$query = preg_replace('/{select}/', $rsm->generateSelectClause(), $query);
$q = self::$em->createNativeQuery($query, $rsm);
foreach ($params as $k => $v) {
$q->setParameter($k, $v);
}
// dump($q);
// die();
return $q->getResult();
}
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', ];