From cf329a20e5bfb3128d125f29c54fa800c0a3a4bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Mon, 1 May 2023 14:29:11 +0200 Subject: [PATCH 1/3] make it possible to use the same table in multiple joins --- src/PersistentData/PersistentDataManager.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/PersistentData/PersistentDataManager.php b/src/PersistentData/PersistentDataManager.php index f87f022..0a75214 100644 --- a/src/PersistentData/PersistentDataManager.php +++ b/src/PersistentData/PersistentDataManager.php @@ -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); } } -- 2.45.2 From e9bfe4e4ad467153655a82a786ffe71513a9c804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Mon, 1 May 2023 14:38:52 +0200 Subject: [PATCH 2/3] use the table name is column names if table is not specified --- src/Database/Query/Select.php | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/Database/Query/Select.php b/src/Database/Query/Select.php index 8acbb0d..4eb53a0 100644 --- a/src/Database/Query/Select.php +++ b/src/Database/Query/Select.php @@ -276,13 +276,12 @@ class Select return [(string) $table, $params]; } - if ($table instanceof Select) - { + if ($table instanceof Select) { return $table->generateQuery(); } if (isset($this->tableAliases[$table])) { - $queryString = ($defineAlias ? Utils::backtick($this->tableAliases[$table]) . ' ' . Utils::backtick($table) : Utils::backtick($table)); + $queryString = $defineAlias ? Utils::backtick($this->tableAliases[$table]) . ' ' . Utils::backtick($table) : Utils::backtick($table); return [$queryString, $params]; } @@ -295,24 +294,17 @@ class Select return (string) $column; } - if (is_array($column)) { - $out = ''; - - if ($column[0]) { - list($tableName, $params) = $this->generateTable($column[0]); - $out .= $tableName . '.'; - } - - $out .= Utils::backtick($column[1]); - - if (!empty($column[2])) { - $out .= ' ' . Utils::backtick($column[2]); - } - - return $out; - } else { - return Utils::backtick($column); + if (!is_array($column)) { + $column = [$this->table, $column]; } + + list($tableName, $params) = $this->generateTable($column[0]); + $out = $tableName . '.' . Utils::backtick($column[1]); + if (!empty($column[2])) { + $out .= ' ' . Utils::backtick($column[2]); + } + + return $out; } private function generateColumns(): string -- 2.45.2 From 70a9e492e3743cf94cccae7760222e14c3131c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Mon, 1 May 2023 16:16:57 +0200 Subject: [PATCH 3/3] make left joins really work --- src/PersistentData/PersistentDataManager.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/PersistentData/PersistentDataManager.php b/src/PersistentData/PersistentDataManager.php index 0a75214..5932f06 100644 --- a/src/PersistentData/PersistentDataManager.php +++ b/src/PersistentData/PersistentDataManager.php @@ -227,12 +227,16 @@ class PersistentDataManager implements IPersistentDataManager next($data); } else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') { - $relationType = current($relations); - $relationModel = new $relationType(); - $this->fillWithData($data, $relationModel, $withRelations, $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); + $method = 'set' . str_replace('_', '', ucwords($relation, '_')); + $model->$method($relationModel); + } else { + next($data); + } next($relations); } else { -- 2.45.2