feature/withrelations-should-contain-names-instead-of-types #13

Merged
bence merged 2 commits from feature/withrelations-should-contain-names-instead-of-types into master 2023-04-30 20:20:27 +02:00
Showing only changes of commit e37ea7c09c - Show all commits

View File

@ -58,53 +58,6 @@ class PersistentDataManager implements IPersistentDataManager
return $this->selectFromDb($select, $type, $useRelations, $withRelations); 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 public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void
{ {
$relations = $model::getRelations(); $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 private function syncRelations(Model $model): void
{ {
foreach ($model::getRelations() as $relation => $relationType) { foreach ($model::getRelations() as $relation => $relationType) {