[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 * from table aliases to class names. Replaces '{select}' in
* $query with the appropriate select list * $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 *')) { if ($_ENV['APP_ENV'] === 'dev' && str_starts_with($query, 'select *')) {
throw new Exception('Cannot use `select *`, use `select {select}` (see ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT)'); throw new Exception('Cannot use `select *`, use `select {select}` (see ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT)');
} }
$rsmb = new ResultSetMappingBuilder(self::$em, ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT); $rsmb = new ResultSetMappingBuilder(self::$em, ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT);
$matches = []; if (\is_null($entities)) {
preg_match_all(self::$table_entity_pattern, $query, $matches); $matches = [];
$entities = []; preg_match_all(self::$table_entity_pattern, $query, $matches);
foreach (F\zip($matches[1], $matches[2]) as [$table, $alias]) { $entities = [];
$entities[$alias] = self::$table_map[$table]; foreach (F\zip($matches[1], $matches[2]) as [$table, $alias]) {
$entities[$alias] = self::$table_map[$table];
}
} }
foreach ($entities as $alias => $entity) { foreach ($entities as $alias => $entity) {
$rsmb->addRootEntityFromClassMetadata($entity, $alias); $rsmb->addRootEntityFromClassMetadata($entity, $alias);