fixup! make it possible to use the same table in multiple joins
All checks were successful
soko-web/pipeline/pr-master This commit looks good

This commit is contained in:
Bence Pőcze 2023-05-01 15:54:25 +02:00
parent 5599519f0f
commit b47ef720d5
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D

View File

@ -148,10 +148,9 @@ class PersistentDataManager implements IPersistentDataManager
$relations = array_intersect_key($relations, array_flip($withRelations));
}
$columns = array_merge($columns, $this->getRelationColumns($relations, $withRelations));
$columns = array_merge($columns, $this->getRelationColumns($table, $relations, $withRelations));
$alreadyUsedTableAliases = [$table => true];
$this->leftJoinRelations($select, $table, $relations, $withRelations, $alreadyUsedTableAliases);
$this->leftJoinRelations($select, $table, $relations, $withRelations);
$select->columns($columns);
} else {
$select->columns($columns);
@ -160,59 +159,43 @@ class PersistentDataManager implements IPersistentDataManager
return $select;
}
private function getRelationColumns(array $relations, array $withRelations): array
private function getRelationColumns(string $table, array $relations, array $withRelations): array
{
$columns = [];
foreach ($relations as $relation => $relationType) {
$relationTableAlias = $table . '__' . $relation;
$relationTable = call_user_func([$relationType, 'getTable']);
foreach (call_user_func([$relationType, 'getFields']) as $relationField) {
$columns[] = [$relationTable, $relationField, $relation . '__' . $relationField];
$columns[] = [$relationTableAlias, $relationField, $relation . '__' . $relationField];
}
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
if (count($withRelations)) {
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
}
$columns = array_merge($columns, $this->getRelationColumns($relationsOfRelation, $withRelations));
$columns = array_merge($columns, $this->getRelationColumns($relationTable, $relationsOfRelation, $withRelations));
}
return $columns;
}
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations, array &$alreadyUsedTableAliases): void
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void
{
foreach ($relations as $relation => $relationType) {
$relationTable = $this->generateTableAlias($select, call_user_func([$relationType, 'getTable']), $alreadyUsedTableAliases);
$select->leftJoin($relationTable, [$relationTable, 'id'], '=', [$table, $relation . '_id']);
$relationTableAlias = $table . '__' . $relation;
$relationTable = call_user_func([$relationType, 'getTable']);
$select->setTableAliases([$relationTableAlias => $relationTable]);
$select->leftJoin($relationTableAlias, [$relationTableAlias, 'id'], '=', [$table, $relation . '_id']);
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
if (count($withRelations)) {
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
}
$this->leftJoinRelations($select, $relationTable, $relationsOfRelation, $withRelations, $alreadyUsedTableAliases);
$this->leftJoinRelations($select, $relationTable, $relationsOfRelation, $withRelations);
}
}
private function generateTableAlias(Select $select, string $table, array &$alreadyUsedTableAliases)
{
if (!isset($alreadyUsedTableAliases[$table])) {
$alreadyUsedTableAliases[$table] = true;
return $table;
}
$i = 2;
do {
$alias = $table[0] . $i;
$i++;
} while (isset($alreadyUsedTableAliases[$alias]));
$select->setTableAliases([$alias => $table]);
$alreadyUsedTableAliases[$alias] = true;
return $alias;
}
private function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
{
$relations = $model::getRelations();