MAPG-156 extend PDM to be able to return with Generator
This commit is contained in:
parent
910fdddf34
commit
70d8807f38
@ -1,5 +1,6 @@
|
|||||||
<?php namespace MapGuesser\PersistentData;
|
<?php namespace MapGuesser\PersistentData;
|
||||||
|
|
||||||
|
use Generator;
|
||||||
use MapGuesser\Database\Query\Modify;
|
use MapGuesser\Database\Query\Modify;
|
||||||
use MapGuesser\Database\Query\Select;
|
use MapGuesser\Database\Query\Select;
|
||||||
use MapGuesser\Interfaces\Database\IResultSet;
|
use MapGuesser\Interfaces\Database\IResultSet;
|
||||||
@ -9,30 +10,8 @@ class PersistentDataManager
|
|||||||
{
|
{
|
||||||
public function selectFromDb(Select $select, string $type, bool $withRelations = false): ?Model
|
public function selectFromDb(Select $select, string $type, bool $withRelations = false): ?Model
|
||||||
{
|
{
|
||||||
$table = call_user_func([$type, 'getTable']);
|
$select = $this->createSelect($select, $type, $withRelations);
|
||||||
$fields = call_user_func([$type, 'getFields']);
|
|
||||||
|
|
||||||
$select->from($table);
|
|
||||||
|
|
||||||
//TODO: only with some relations?
|
|
||||||
if ($withRelations) {
|
|
||||||
$relations = call_user_func([$type, 'getRelations']);
|
|
||||||
|
|
||||||
$columns = [];
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: return with array?
|
|
||||||
$data = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
$data = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
||||||
|
|
||||||
if ($data === null) {
|
if ($data === null) {
|
||||||
@ -45,6 +24,18 @@ class PersistentDataManager
|
|||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function selectMultipleFromDb(Select $select, string $type, bool $withRelations = false): Generator
|
||||||
|
{
|
||||||
|
$select = $this->createSelect($select, $type, $withRelations);
|
||||||
|
|
||||||
|
while ($data = $select->execute()->fetch(IResultSet::FETCH_ASSOC)) {
|
||||||
|
$model = new $type();
|
||||||
|
$this->fillWithData($data, $model);
|
||||||
|
|
||||||
|
yield $model;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function selectFromDbById($id, string $type, bool $withRelations = false): ?Model
|
public function selectFromDbById($id, string $type, bool $withRelations = false): ?Model
|
||||||
{
|
{
|
||||||
$select = new Select(\Container::$dbConnection);
|
$select = new Select(\Container::$dbConnection);
|
||||||
@ -136,6 +127,34 @@ class PersistentDataManager
|
|||||||
$model->resetSnapshot();
|
$model->resetSnapshot();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function createSelect(Select $select, string $type, bool $withRelations = false): Select
|
||||||
|
{
|
||||||
|
$table = call_user_func([$type, 'getTable']);
|
||||||
|
$fields = call_user_func([$type, 'getFields']);
|
||||||
|
|
||||||
|
$select->from($table);
|
||||||
|
|
||||||
|
//TODO: only with some relations?
|
||||||
|
if ($withRelations) {
|
||||||
|
$relations = call_user_func([$type, 'getRelations']);
|
||||||
|
|
||||||
|
$columns = [];
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $select;
|
||||||
|
}
|
||||||
|
|
||||||
private function getRelationColumns(array $relations): array
|
private function getRelationColumns(array $relations): array
|
||||||
{
|
{
|
||||||
$columns = [];
|
$columns = [];
|
||||||
|
Loading…
Reference in New Issue
Block a user