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
All checks were successful
soko-web/pipeline/pr-master This commit looks good
This commit is contained in:
parent
5599519f0f
commit
b47ef720d5
@ -148,10 +148,9 @@ class PersistentDataManager implements IPersistentDataManager
|
|||||||
$relations = array_intersect_key($relations, array_flip($withRelations));
|
$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);
|
||||||
$this->leftJoinRelations($select, $table, $relations, $withRelations, $alreadyUsedTableAliases);
|
|
||||||
$select->columns($columns);
|
$select->columns($columns);
|
||||||
} else {
|
} else {
|
||||||
$select->columns($columns);
|
$select->columns($columns);
|
||||||
@ -160,59 +159,43 @@ class PersistentDataManager implements IPersistentDataManager
|
|||||||
return $select;
|
return $select;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getRelationColumns(array $relations, array $withRelations): array
|
private function getRelationColumns(string $table, array $relations, array $withRelations): array
|
||||||
{
|
{
|
||||||
$columns = [];
|
$columns = [];
|
||||||
|
|
||||||
foreach ($relations as $relation => $relationType) {
|
foreach ($relations as $relation => $relationType) {
|
||||||
|
$relationTableAlias = $table . '__' . $relation;
|
||||||
$relationTable = call_user_func([$relationType, 'getTable']);
|
$relationTable = call_user_func([$relationType, 'getTable']);
|
||||||
foreach (call_user_func([$relationType, 'getFields']) as $relationField) {
|
foreach (call_user_func([$relationType, 'getFields']) as $relationField) {
|
||||||
$columns[] = [$relationTable, $relationField, $relation . '__' . $relationField];
|
$columns[] = [$relationTableAlias, $relationField, $relation . '__' . $relationField];
|
||||||
}
|
}
|
||||||
|
|
||||||
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
||||||
if (count($withRelations)) {
|
if (count($withRelations)) {
|
||||||
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($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;
|
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) {
|
foreach ($relations as $relation => $relationType) {
|
||||||
$relationTable = $this->generateTableAlias($select, call_user_func([$relationType, 'getTable']), $alreadyUsedTableAliases);
|
$relationTableAlias = $table . '__' . $relation;
|
||||||
$select->leftJoin($relationTable, [$relationTable, 'id'], '=', [$table, $relation . '_id']);
|
$relationTable = call_user_func([$relationType, 'getTable']);
|
||||||
|
$select->setTableAliases([$relationTableAlias => $relationTable]);
|
||||||
|
$select->leftJoin($relationTableAlias, [$relationTableAlias, 'id'], '=', [$table, $relation . '_id']);
|
||||||
|
|
||||||
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
||||||
if (count($withRelations)) {
|
if (count($withRelations)) {
|
||||||
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($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
|
private function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
|
||||||
{
|
{
|
||||||
$relations = $model::getRelations();
|
$relations = $model::getRelations();
|
||||||
|
Loading…
Reference in New Issue
Block a user