feature/use-the-same-table-in-multiple-joins #14

Merged
bence merged 3 commits from feature/use-the-same-table-in-multiple-joins into master 2023-05-01 19:08:23 +02:00
Showing only changes of commit cf329a20e5 - Show all commits

View File

@ -148,7 +148,7 @@ 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));
$this->leftJoinRelations($select, $table, $relations, $withRelations);
$select->columns($columns);
@ -159,21 +159,22 @@ 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;
@ -182,14 +183,16 @@ class PersistentDataManager implements IPersistentDataManager
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void
{
foreach ($relations as $relation => $relationType) {
$relationTableAlias = $table . '__' . $relation;
$relationTable = call_user_func([$relationType, 'getTable']);
$select->leftJoin($relationTable, [$relationTable, 'id'], '=', [$table, $relation . '_id']);
$select->setTableAliases([$relationTableAlias => $relationTable]);
$select->leftJoin($relationTableAlias, [$relationTableAlias, 'id'], '=', [$table, $relation . '_id']);
$nextOrderRelations = call_user_func([$relationType, 'getRelations']);
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
if (count($withRelations)) {
$nextOrderRelations = array_intersect_key($nextOrderRelations, array_flip($withRelations));
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
}
$this->leftJoinRelations($select, $relationTable, $nextOrderRelations, $withRelations);
$this->leftJoinRelations($select, $relationTable, $relationsOfRelation, $withRelations);
}
}