[CORE][DB] Allow specifying the entites to be retrieved, as when using renaming, tables in join would attempt to be selected

This commit is contained in:
Hugo Sales 2021-11-28 18:53:34 +00:00 committed by Diogo Peralta Cordeiro
parent 5c3d561a67
commit 3227018963
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0

View File

@ -121,17 +121,19 @@ class DB
* from table aliases to class names. Replaces '{select}' in
* $query with the appropriate select list
*/
public static function sql(string $query, array $params = [])
public static function sql(string $query, array $params = [], ?array $entities = null)
{
if ($_ENV['APP_ENV'] === 'dev' && str_starts_with($query, 'select *')) {
throw new Exception('Cannot use `select *`, use `select {select}` (see ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT)');
}
$rsmb = new ResultSetMappingBuilder(self::$em, ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT);
$matches = [];
preg_match_all(self::$table_entity_pattern, $query, $matches);
$entities = [];
foreach (F\zip($matches[1], $matches[2]) as [$table, $alias]) {
$entities[$alias] = self::$table_map[$table];
$rsmb = new ResultSetMappingBuilder(self::$em, ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT);
if (\is_null($entities)) {
$matches = [];
preg_match_all(self::$table_entity_pattern, $query, $matches);
$entities = [];
foreach (F\zip($matches[1], $matches[2]) as [$table, $alias]) {
$entities[$alias] = self::$table_map[$table];
}
}
foreach ($entities as $alias => $entity) {
$rsmb->addRootEntityFromClassMetadata($entity, $alias);