Compare commits

...

2 Commits

Author SHA1 Message Date
9e3e2f96b2
make left joins really work
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-05-01 16:30:21 +02:00
b4589fd7b6
fixup! make it possible to use the same table in multiple joins 2023-05-01 16:30:21 +02:00

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();
@ -244,12 +227,16 @@ class PersistentDataManager implements IPersistentDataManager
next($data);
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
if ($data[$relation . '__id'] !== null) {
$relationType = current($relations);
$relationModel = new $relationType();
$this->fillWithData($data, $relationModel, $withRelations, $relation);
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
$model->$method($relationModel);
} else {
next($data);
}
next($relations);
} else {