MAPG-235 queries with relations on any recursion level implemented
This commit is contained in:
1 parent
21e41b7c36
commit
44df94eb98
6 files changed
+102
-26
No files matched your search
@@ -45,24 +45,62 @@ class PersistentDataManager
|
||||
return $this->selectFromDb($select, $type, $withRelations);
|
||||
}
|
||||
|
||||
public function fillWithData(array $data, Model $model): void
|
||||
public function fillWithData(array &$data, Model $model, ?string $modelKey = null): void
|
||||
{
|
||||
$relations = $model::getRelations();
|
||||
$relationData = [];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if ($this->extractRelationData($key, $value, $relationData, $relations)) {
|
||||
continue;
|
||||
}
|
||||
while (key($data)) {
|
||||
$key = key($data);
|
||||
$value = current($data);
|
||||
$relation = key($relations);
|
||||
|
||||
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
|
||||
if (strpos($key, '__') == false) {
|
||||
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
|
||||
|
||||
if (method_exists($model, $method)) {
|
||||
$model->$method($value);
|
||||
if (method_exists($model, $method)) {
|
||||
$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)) {
|
||||
$model->$method($value);
|
||||
}
|
||||
|
||||
next($data);
|
||||
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
|
||||
// $relation = current($relations);
|
||||
$relationType = current($relations);
|
||||
$relationModel = new $relationType();
|
||||
$this->fillWithData($data, $relationModel, $relation);
|
||||
|
||||
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
|
||||
$model->$method($relationModel);
|
||||
|
||||
next($relations);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->setRelations($model, $relationData);
|
||||
// foreach ($data as $key => $value) {
|
||||
// if ($this->extractRelationData($key, $value, $relationData, $relations)) {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $method = 'set' . str_replace('_', '', ucwords($key, '_'));
|
||||
|
||||
// if (method_exists($model, $method)) {
|
||||
// $model->$method($value);
|
||||
// }
|
||||
// }
|
||||
|
||||
// $this->setRelations($model, $relationData);
|
||||
|
||||
$model->saveSnapshot();
|
||||
}
|
||||
@@ -133,9 +171,15 @@ class PersistentDataManager
|
||||
$table = call_user_func([$type, 'getTable']);
|
||||
$fields = call_user_func([$type, 'getFields']);
|
||||
|
||||
array_walk($fields, function (&$value, $key, $table) {
|
||||
$value = [$table, $value];
|
||||
}, $table);
|
||||
// array_walk($fields, function (&$value, $key, $table) {
|
||||
// $value = [$table, $value];
|
||||
// }, $table);
|
||||
|
||||
$columns = [];
|
||||
|
||||
foreach ($fields as $field) {
|
||||
$columns[] = [$table, $field];
|
||||
}
|
||||
|
||||
$select->from($table);
|
||||
|
||||
@@ -143,18 +187,19 @@ class PersistentDataManager
|
||||
if ($withRelations) {
|
||||
$relations = call_user_func([$type, 'getRelations']);
|
||||
|
||||
$columns = [];
|
||||
// $columns = [];
|
||||
|
||||
foreach ($fields as $field) {
|
||||
$columns[] = [$table, $field];
|
||||
}
|
||||
// foreach ($fields as $field) {
|
||||
// $columns[] = [$table, $field];
|
||||
// }
|
||||
|
||||
$columns = array_merge($columns, $this->getRelationColumns($relations));
|
||||
|
||||
$this->leftJoinRelations($select, $table, $relations);
|
||||
$select->columns($columns);
|
||||
} else {
|
||||
$select->columns($fields);
|
||||
// $select->columns($fields);
|
||||
$select->columns($columns);
|
||||
}
|
||||
|
||||
return $select;
|
||||
@@ -169,6 +214,9 @@ class PersistentDataManager
|
||||
foreach (call_user_func([$relationType, 'getFields']) as $relationField) {
|
||||
$columns[] = [$relationTable, $relationField, $relation . '__' . $relationField];
|
||||
}
|
||||
|
||||
$nextOrderRelations = call_user_func([$relationType, 'getRelations']);
|
||||
$columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations));
|
||||
}
|
||||
|
||||
return $columns;
|
||||
@@ -179,6 +227,9 @@ class PersistentDataManager
|
||||
foreach ($relations as $relation => $relationType) {
|
||||
$relationTable = call_user_func([$relationType, 'getTable']);
|
||||
$select->leftJoin($relationTable, [$relationTable, 'id'], '=', [$table, $relation . '_id']);
|
||||
|
||||
$nextOrderRelations = call_user_func([$relationType, 'getRelations']);
|
||||
$this->leftJoinRelations($select, $relationTable, $nextOrderRelations);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user