[DB] Add wrapper for making native queries
This commit is contained in:
parent
a98af6ab6a
commit
80cefca90d
@ -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', ];
|
||||
|
Loading…
Reference in New Issue
Block a user