MAPG-235 queries with selected relations enabled; reduced size of some of the queries
This commit is contained in:
1 parent
7898c52328
commit
a7cfc3c19f
7 files changed
+67
-103
No files matched your search
@@ -8,9 +8,9 @@ use MapGuesser\PersistentData\Model\Model;
|
||||
|
||||
class PersistentDataManager
|
||||
{
|
||||
public function selectFromDb(Select $select, string $type, bool $withRelations = false)
|
||||
public function selectFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = [])
|
||||
{
|
||||
$select = $this->createSelect($select, $type, $withRelations);
|
||||
$select = $this->createSelect($select, $type, $useRelations, $withRelations);
|
||||
|
||||
$data = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
||||
|
||||
@@ -19,36 +19,38 @@ class PersistentDataManager
|
||||
}
|
||||
|
||||
$model = new $type();
|
||||
$this->fillWithData($data, $model);
|
||||
$this->fillWithData($data, $model, $withRelations);
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function selectMultipleFromDb(Select $select, string $type, bool $withRelations = false): Generator
|
||||
public function selectMultipleFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Generator
|
||||
{
|
||||
$select = $this->createSelect($select, $type, $withRelations);
|
||||
$select = $this->createSelect($select, $type, $useRelations, $withRelations);
|
||||
$result = $select->execute();
|
||||
|
||||
while ($data = $result->fetch(IResultSet::FETCH_ASSOC)) {
|
||||
$model = new $type();
|
||||
$this->fillWithData($data, $model);
|
||||
$this->fillWithData($data, $model, $withRelations);
|
||||
|
||||
yield $model;
|
||||
}
|
||||
}
|
||||
|
||||
public function selectFromDbById($id, string $type, bool $withRelations = false)
|
||||
public function selectFromDbById($id, string $type, bool $useRelations = false)
|
||||
{
|
||||
$select = new Select(\Container::$dbConnection);
|
||||
$select->whereId($id);
|
||||
|
||||
return $this->selectFromDb($select, $type, $withRelations);
|
||||
return $this->selectFromDb($select, $type, $useRelations);
|
||||
}
|
||||
|
||||
public function fillWithData(array &$data, Model $model, ?string $modelKey = null): void
|
||||
public function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
|
||||
{
|
||||
$relations = $model::getRelations();
|
||||
$relationData = [];
|
||||
if (count($withRelations)) {
|
||||
$relations = array_intersect($relations, $withRelations);
|
||||
}
|
||||
|
||||
while (key($data)) {
|
||||
$key = key($data);
|
||||
@@ -74,34 +76,19 @@ class PersistentDataManager
|
||||
|
||||
next($data);
|
||||
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
|
||||
// $relation = current($relations);
|
||||
$relationType = current($relations);
|
||||
$relationModel = new $relationType();
|
||||
$this->fillWithData($data, $relationModel, $relation);
|
||||
$this->fillWithData($data, $relationModel, $withRelations, $relation);
|
||||
|
||||
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
|
||||
$model->$method($relationModel);
|
||||
|
||||
next($relations);
|
||||
} else {
|
||||
break;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
@@ -166,15 +153,11 @@ class PersistentDataManager
|
||||
$model->resetSnapshot();
|
||||
}
|
||||
|
||||
private function createSelect(Select $select, string $type, bool $withRelations = false): Select
|
||||
private function createSelect(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Select
|
||||
{
|
||||
$table = call_user_func([$type, 'getTable']);
|
||||
$fields = call_user_func([$type, 'getFields']);
|
||||
|
||||
// array_walk($fields, function (&$value, $key, $table) {
|
||||
// $value = [$table, $value];
|
||||
// }, $table);
|
||||
|
||||
$columns = [];
|
||||
|
||||
foreach ($fields as $field) {
|
||||
@@ -183,29 +166,24 @@ class PersistentDataManager
|
||||
|
||||
$select->from($table);
|
||||
|
||||
//TODO: only with some relations?
|
||||
if ($withRelations) {
|
||||
if ($useRelations) {
|
||||
$relations = call_user_func([$type, 'getRelations']);
|
||||
if (count($withRelations)) {
|
||||
$relations = array_intersect($relations, $withRelations);
|
||||
}
|
||||
|
||||
// $columns = [];
|
||||
$columns = array_merge($columns, $this->getRelationColumns($relations, $withRelations));
|
||||
|
||||
// foreach ($fields as $field) {
|
||||
// $columns[] = [$table, $field];
|
||||
// }
|
||||
|
||||
$columns = array_merge($columns, $this->getRelationColumns($relations));
|
||||
|
||||
$this->leftJoinRelations($select, $table, $relations);
|
||||
$this->leftJoinRelations($select, $table, $relations, $withRelations);
|
||||
$select->columns($columns);
|
||||
} else {
|
||||
// $select->columns($fields);
|
||||
$select->columns($columns);
|
||||
}
|
||||
|
||||
return $select;
|
||||
}
|
||||
|
||||
private function getRelationColumns(array $relations): array
|
||||
private function getRelationColumns(array $relations, array $withRelations): array
|
||||
{
|
||||
$columns = [];
|
||||
|
||||
@@ -216,50 +194,26 @@ class PersistentDataManager
|
||||
}
|
||||
|
||||
$nextOrderRelations = call_user_func([$relationType, 'getRelations']);
|
||||
$columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations));
|
||||
if (count($withRelations)) {
|
||||
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
|
||||
}
|
||||
$columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations, $withRelations));
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
private function leftJoinRelations(Select $select, string $table, array $relations): void
|
||||
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private function extractRelationData(string $key, $value, array &$relationData, array $relations): bool
|
||||
{
|
||||
$found = false;
|
||||
|
||||
foreach ($relations as $relation => $relationType) {
|
||||
if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
|
||||
$found = true;
|
||||
$relationData[$relation][substr($key, strlen($relation . '__'))] = $value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $found;
|
||||
}
|
||||
|
||||
private function setRelations(Model $model, array &$relations): void
|
||||
{
|
||||
foreach ($model::getRelations() as $relation => $relationType) {
|
||||
if (isset($relations[$relation])) {
|
||||
$object = new $relationType();
|
||||
|
||||
$this->fillWithData($relations[$relation], $object);
|
||||
|
||||
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
|
||||
|
||||
$model->$method($object);
|
||||
if (count($withRelations)) {
|
||||
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
|
||||
}
|
||||
$this->leftJoinRelations($select, $relationTable, $nextOrderRelations, $withRelations);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user