[CORE][DB] Make DB::sql not error when selecting from tables with columns of the same name and remove the second enitites paramter, calculating it internally instead

This commit is contained in:
Hugo Sales 2021-11-08 20:15:45 +00:00
parent f98afd15ce
commit 1d84f1629e
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -64,6 +64,7 @@ class DB
*/
private static array $table_map = [];
private static array $class_pk = [];
private static ?string $table_entity_pattern = null;
public static function initTableMap()
{
$all = self::$em->getMetadataFactory()->getAllMetadata();
@ -71,6 +72,8 @@ class DB
self::$table_map[$meta->getTableName()] = $meta->getMetadataValue('name');
self::$class_pk[$meta->getMetadataValue('name')] = $meta->getIdentifier();
}
self::$table_entity_pattern = '/(' . implode('|', array_keys(self::$table_map)) . ')\s([^\s]+)/';
}
public static function getTableForClass(string $class)
@ -111,9 +114,16 @@ class DB
* from table aliases to class names. Replaces '{select}' in
* $query with the appropriate select list
*/
public static function sql(string $query, array $entities, array $params = [])
public static function sql(string $query// , array $entities
, array $params = [], )
{
$rsm = new ResultSetMappingBuilder(self::$em);
$rsm = 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];
}
foreach ($entities as $alias => $entity) {
$rsm->addRootEntityFromClassMetadata($entity, $alias);
}