Merge pull request 'feature/withrelations-should-contain-names-instead-of-types' (#13) from feature/withrelations-should-contain-names-instead-of-types into master
All checks were successful
soko-web/pipeline/head This commit looks good

Reviewed-on: #13
This commit is contained in:
Bence Pőcze 2023-04-30 20:20:27 +02:00
commit 88a2a99527
Signed by: Gitea
GPG Key ID: 7B89B83EED9AD2C6
2 changed files with 60 additions and 55 deletions

View File

@ -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;

View File

@ -50,64 +50,22 @@ 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
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void
{
$relations = $model::getRelations();
if (count($withRelations)) {
$relations = array_intersect($relations, $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): void
{
foreach ($model::getRelations() as $relation => $relationType) {
foreach ($relations as $relation => $relationType) {
$camel = str_replace('_', '', ucwords($relation, '_'));
$methodGet = 'get' . $camel . 'Id';
@ -116,7 +74,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 +145,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 +169,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,12 +187,59 @@ 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);
}
}
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) {