From ad7ea0de9d3cb620043e883ca366468e4c329374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 30 Apr 2023 19:19:00 +0200 Subject: [PATCH 1/2] withRelations in methods of PersistentDataManager should contain relation names instead of types --- .../PersistentData/IPersistentDataManager.php | 4 +-- src/PersistentData/PersistentDataManager.php | 27 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/Interfaces/PersistentData/IPersistentDataManager.php b/src/Interfaces/PersistentData/IPersistentDataManager.php index c6c931d..7978fc7 100644 --- a/src/Interfaces/PersistentData/IPersistentDataManager.php +++ b/src/Interfaces/PersistentData/IPersistentDataManager.php @@ -10,9 +10,9 @@ interface IPersistentDataManager public function selectMultipleFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Generator; - public function selectFromDbById($id, string $type, bool $useRelations = false); + public function selectFromDbById($id, string $type, bool $useRelations = false, array $withRelations = []); - public function loadRelationsFromDb(Model $model, bool $recursive): void; + public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void; public function saveToDb(Model $model): void; diff --git a/src/PersistentData/PersistentDataManager.php b/src/PersistentData/PersistentDataManager.php index 4388d9f..c203b8b 100644 --- a/src/PersistentData/PersistentDataManager.php +++ b/src/PersistentData/PersistentDataManager.php @@ -50,19 +50,19 @@ class PersistentDataManager implements IPersistentDataManager } } - public function selectFromDbById($id, string $type, bool $useRelations = false) + public function selectFromDbById($id, string $type, bool $useRelations = false, array $withRelations = []) { $select = new Select($this->dbConnection); $select->whereId($id); - return $this->selectFromDb($select, $type, $useRelations); + return $this->selectFromDb($select, $type, $useRelations, $withRelations); } public function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void { $relations = $model::getRelations(); if (count($withRelations)) { - $relations = array_intersect($relations, $withRelations); + $relations = array_intersect_key($relations, array_flip($withRelations)); } while (key($data)) { @@ -105,9 +105,14 @@ class PersistentDataManager implements IPersistentDataManager $model->saveSnapshot(); } - public function loadRelationsFromDb(Model $model, bool $recursive): void + public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void { - foreach ($model::getRelations() as $relation => $relationType) { + $relations = $model::getRelations(); + if (count($withRelations)) { + $relations = array_intersect_key($relations, array_flip($withRelations)); + } + + foreach ($relations as $relation => $relationType) { $camel = str_replace('_', '', ucwords($relation, '_')); $methodGet = 'get' . $camel . 'Id'; @@ -116,7 +121,7 @@ class PersistentDataManager implements IPersistentDataManager $relationId = $model->$methodGet(); if ($relationId !== null) { - $relationModel = $this->selectFromDbById($relationId, $relationType, $recursive); + $relationModel = $this->selectFromDbById($relationId, $relationType, $recursive, $withRelations); $model->$methodSet($relationModel); } @@ -187,7 +192,7 @@ class PersistentDataManager implements IPersistentDataManager if ($useRelations) { $relations = call_user_func([$type, 'getRelations']); if (count($withRelations)) { - $relations = array_intersect($relations, $withRelations); + $relations = array_intersect_key($relations, array_flip($withRelations)); } $columns = array_merge($columns, $this->getRelationColumns($relations, $withRelations)); @@ -211,11 +216,11 @@ class PersistentDataManager implements IPersistentDataManager $columns[] = [$relationTable, $relationField, $relation . '__' . $relationField]; } - $nextOrderRelations = call_user_func([$relationType, 'getRelations']); + $relationsOfRelation = call_user_func([$relationType, 'getRelations']); if (count($withRelations)) { - $nextOrderRelations = array_intersect($nextOrderRelations, $withRelations); + $relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations)); } - $columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations, $withRelations)); + $columns = array_merge($columns, $this->getRelationColumns($relationsOfRelation, $withRelations)); } return $columns; @@ -229,7 +234,7 @@ class PersistentDataManager implements IPersistentDataManager $nextOrderRelations = call_user_func([$relationType, 'getRelations']); if (count($withRelations)) { - $nextOrderRelations = array_intersect($nextOrderRelations, $withRelations); + $nextOrderRelations = array_intersect_key($nextOrderRelations, array_flip($withRelations)); } $this->leftJoinRelations($select, $relationTable, $nextOrderRelations, $withRelations); } -- 2.45.2 From e37ea7c09c0a3257644c3c3b92c40f7ad5cfcb75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 30 Apr 2023 19:20:25 +0200 Subject: [PATCH 2/2] make PersistentDataManager::$fillWithData private --- src/PersistentData/PersistentDataManager.php | 94 ++++++++++---------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/src/PersistentData/PersistentDataManager.php b/src/PersistentData/PersistentDataManager.php index c203b8b..f87f022 100644 --- a/src/PersistentData/PersistentDataManager.php +++ b/src/PersistentData/PersistentDataManager.php @@ -58,53 +58,6 @@ class PersistentDataManager implements IPersistentDataManager return $this->selectFromDb($select, $type, $useRelations, $withRelations); } - public function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void - { - $relations = $model::getRelations(); - if (count($withRelations)) { - $relations = array_intersect_key($relations, array_flip($withRelations)); - } - - while (key($data)) { - $key = key($data); - $value = current($data); - $relation = key($relations); - - if (strpos($key, '__') === false) { - $method = 'set' . str_replace('_', '', ucwords($key, '_')); - - if (method_exists($model, $method) && isset($value)) { - $model->$method($value); - } - - next($data); - } else if (isset($modelKey) && substr($key, 0, strlen($modelKey . '__')) === $modelKey . '__') { - $key = substr($key, strlen($modelKey) + 2); - - $method = 'set' . str_replace('_', '', ucwords($key, '_')); - - if (method_exists($model, $method) && isset($value)) { - $model->$method($value); - } - - next($data); - } else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') { - $relationType = current($relations); - $relationModel = new $relationType(); - $this->fillWithData($data, $relationModel, $withRelations, $relation); - - $method = 'set' . str_replace('_', '', ucwords($relation, '_')); - $model->$method($relationModel); - - next($relations); - } else { - return; - } - } - - $model->saveSnapshot(); - } - public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void { $relations = $model::getRelations(); @@ -240,6 +193,53 @@ class PersistentDataManager implements IPersistentDataManager } } + private function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void + { + $relations = $model::getRelations(); + if (count($withRelations)) { + $relations = array_intersect_key($relations, array_flip($withRelations)); + } + + while (key($data)) { + $key = key($data); + $value = current($data); + $relation = key($relations); + + if (strpos($key, '__') === false) { + $method = 'set' . str_replace('_', '', ucwords($key, '_')); + + if (method_exists($model, $method) && isset($value)) { + $model->$method($value); + } + + next($data); + } else if (isset($modelKey) && substr($key, 0, strlen($modelKey . '__')) === $modelKey . '__') { + $key = substr($key, strlen($modelKey) + 2); + + $method = 'set' . str_replace('_', '', ucwords($key, '_')); + + if (method_exists($model, $method) && isset($value)) { + $model->$method($value); + } + + next($data); + } else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') { + $relationType = current($relations); + $relationModel = new $relationType(); + $this->fillWithData($data, $relationModel, $withRelations, $relation); + + $method = 'set' . str_replace('_', '', ucwords($relation, '_')); + $model->$method($relationModel); + + next($relations); + } else { + return; + } + } + + $model->saveSnapshot(); + } + private function syncRelations(Model $model): void { foreach ($model::getRelations() as $relation => $relationType) { -- 2.45.2