Compare commits

..

No commits in common. "88a2a99527b51dfb240ec78ac7070dc36a1022b6" and "445774e59a4891b2e67d151b0cf7b7b880c40e48" have entirely different histories.

2 changed files with 55 additions and 60 deletions

View File

@ -10,9 +10,9 @@ interface IPersistentDataManager
public function selectMultipleFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Generator; public function selectMultipleFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Generator;
public function selectFromDbById($id, string $type, bool $useRelations = false, array $withRelations = []); public function selectFromDbById($id, string $type, bool $useRelations = false);
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void; public function loadRelationsFromDb(Model $model, bool $recursive): void;
public function saveToDb(Model $model): void; public function saveToDb(Model $model): void;

View File

@ -50,22 +50,64 @@ class PersistentDataManager implements IPersistentDataManager
} }
} }
public function selectFromDbById($id, string $type, bool $useRelations = false, array $withRelations = []) public function selectFromDbById($id, string $type, bool $useRelations = false)
{ {
$select = new Select($this->dbConnection); $select = new Select($this->dbConnection);
$select->whereId($id); $select->whereId($id);
return $this->selectFromDb($select, $type, $useRelations, $withRelations); return $this->selectFromDb($select, $type, $useRelations);
} }
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void public function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
{ {
$relations = $model::getRelations(); $relations = $model::getRelations();
if (count($withRelations)) { if (count($withRelations)) {
$relations = array_intersect_key($relations, array_flip($withRelations)); $relations = array_intersect($relations, $withRelations);
} }
foreach ($relations as $relation => $relationType) { 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) {
$camel = str_replace('_', '', ucwords($relation, '_')); $camel = str_replace('_', '', ucwords($relation, '_'));
$methodGet = 'get' . $camel . 'Id'; $methodGet = 'get' . $camel . 'Id';
@ -74,7 +116,7 @@ class PersistentDataManager implements IPersistentDataManager
$relationId = $model->$methodGet(); $relationId = $model->$methodGet();
if ($relationId !== null) { if ($relationId !== null) {
$relationModel = $this->selectFromDbById($relationId, $relationType, $recursive, $withRelations); $relationModel = $this->selectFromDbById($relationId, $relationType, $recursive);
$model->$methodSet($relationModel); $model->$methodSet($relationModel);
} }
@ -145,7 +187,7 @@ class PersistentDataManager implements IPersistentDataManager
if ($useRelations) { if ($useRelations) {
$relations = call_user_func([$type, 'getRelations']); $relations = call_user_func([$type, 'getRelations']);
if (count($withRelations)) { if (count($withRelations)) {
$relations = array_intersect_key($relations, array_flip($withRelations)); $relations = array_intersect($relations, $withRelations);
} }
$columns = array_merge($columns, $this->getRelationColumns($relations, $withRelations)); $columns = array_merge($columns, $this->getRelationColumns($relations, $withRelations));
@ -169,11 +211,11 @@ class PersistentDataManager implements IPersistentDataManager
$columns[] = [$relationTable, $relationField, $relation . '__' . $relationField]; $columns[] = [$relationTable, $relationField, $relation . '__' . $relationField];
} }
$relationsOfRelation = call_user_func([$relationType, 'getRelations']); $nextOrderRelations = call_user_func([$relationType, 'getRelations']);
if (count($withRelations)) { if (count($withRelations)) {
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations)); $nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
} }
$columns = array_merge($columns, $this->getRelationColumns($relationsOfRelation, $withRelations)); $columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations, $withRelations));
} }
return $columns; return $columns;
@ -187,59 +229,12 @@ class PersistentDataManager implements IPersistentDataManager
$nextOrderRelations = call_user_func([$relationType, 'getRelations']); $nextOrderRelations = call_user_func([$relationType, 'getRelations']);
if (count($withRelations)) { if (count($withRelations)) {
$nextOrderRelations = array_intersect_key($nextOrderRelations, array_flip($withRelations)); $nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
} }
$this->leftJoinRelations($select, $relationTable, $nextOrderRelations, $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 private function syncRelations(Model $model): void
{ {
foreach ($model::getRelations() as $relation => $relationType) { foreach ($model::getRelations() as $relation => $relationType) {