Compare commits
7
Commits
v0.6.1
...
219b42f995
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
219b42f995
|
||
|
|
70a9e492e3
|
||
|
|
e9bfe4e4ad
|
||
|
|
cf329a20e5
|
||
|
|
88a2a99527
|
||
|
|
e37ea7c09c
|
||
|
|
ad7ea0de9d
|
No files matched your search
@@ -276,13 +276,12 @@ class Select
|
||||
return [(string) $table, $params];
|
||||
}
|
||||
|
||||
if ($table instanceof Select)
|
||||
{
|
||||
if ($table instanceof Select) {
|
||||
return $table->generateQuery();
|
||||
}
|
||||
|
||||
if (isset($this->tableAliases[$table])) {
|
||||
$queryString = ($defineAlias ? Utils::backtick($this->tableAliases[$table]) . ' ' . Utils::backtick($table) : Utils::backtick($table));
|
||||
$queryString = $defineAlias ? Utils::backtick($this->tableAliases[$table]) . ' ' . Utils::backtick($table) : Utils::backtick($table);
|
||||
return [$queryString, $params];
|
||||
}
|
||||
|
||||
@@ -295,24 +294,17 @@ class Select
|
||||
return (string) $column;
|
||||
}
|
||||
|
||||
if (is_array($column)) {
|
||||
$out = '';
|
||||
|
||||
if ($column[0]) {
|
||||
list($tableName, $params) = $this->generateTable($column[0]);
|
||||
$out .= $tableName . '.';
|
||||
}
|
||||
|
||||
$out .= Utils::backtick($column[1]);
|
||||
|
||||
if (!empty($column[2])) {
|
||||
$out .= ' ' . Utils::backtick($column[2]);
|
||||
}
|
||||
|
||||
return $out;
|
||||
} else {
|
||||
return Utils::backtick($column);
|
||||
if (!is_array($column)) {
|
||||
$column = [$this->table, $column];
|
||||
}
|
||||
|
||||
list($tableName, $params) = $this->generateTable($column[0]);
|
||||
$out = $tableName . '.' . Utils::backtick($column[1]);
|
||||
if (!empty($column[2])) {
|
||||
$out .= ' ' . Utils::backtick($column[2]);
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
private function generateColumns(): string
|
||||
|
||||
@@ -10,9 +10,9 @@ interface IPersistentDataManager
|
||||
|
||||
public function selectMultipleFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Generator;
|
||||
|
||||
public function selectFromDbById($id, string $type, bool $useRelations = false);
|
||||
public function selectFromDbById($id, string $type, bool $useRelations = false, array $withRelations = []);
|
||||
|
||||
public function loadRelationsFromDb(Model $model, bool $recursive): void;
|
||||
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void;
|
||||
|
||||
public function saveToDb(Model $model): void;
|
||||
|
||||
|
||||
@@ -50,64 +50,22 @@ class PersistentDataManager implements IPersistentDataManager
|
||||
}
|
||||
}
|
||||
|
||||
public function selectFromDbById($id, string $type, bool $useRelations = false)
|
||||
public function selectFromDbById($id, string $type, bool $useRelations = false, array $withRelations = [])
|
||||
{
|
||||
$select = new Select($this->dbConnection);
|
||||
$select->whereId($id);
|
||||
|
||||
return $this->selectFromDb($select, $type, $useRelations);
|
||||
return $this->selectFromDb($select, $type, $useRelations, $withRelations);
|
||||
}
|
||||
|
||||
public function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
|
||||
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void
|
||||
{
|
||||
$relations = $model::getRelations();
|
||||
if (count($withRelations)) {
|
||||
$relations = array_intersect($relations, $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): void
|
||||
{
|
||||
foreach ($model::getRelations() as $relation => $relationType) {
|
||||
foreach ($relations as $relation => $relationType) {
|
||||
$camel = str_replace('_', '', ucwords($relation, '_'));
|
||||
|
||||
$methodGet = 'get' . $camel . 'Id';
|
||||
@@ -116,7 +74,7 @@ class PersistentDataManager implements IPersistentDataManager
|
||||
$relationId = $model->$methodGet();
|
||||
|
||||
if ($relationId !== null) {
|
||||
$relationModel = $this->selectFromDbById($relationId, $relationType, $recursive);
|
||||
$relationModel = $this->selectFromDbById($relationId, $relationType, $recursive, $withRelations);
|
||||
|
||||
$model->$methodSet($relationModel);
|
||||
}
|
||||
@@ -187,10 +145,10 @@ class PersistentDataManager implements IPersistentDataManager
|
||||
if ($useRelations) {
|
||||
$relations = call_user_func([$type, 'getRelations']);
|
||||
if (count($withRelations)) {
|
||||
$relations = array_intersect($relations, $withRelations);
|
||||
$relations = array_intersect_key($relations, array_flip($withRelations));
|
||||
}
|
||||
|
||||
$columns = array_merge($columns, $this->getRelationColumns($relations, $withRelations));
|
||||
$columns = array_merge($columns, $this->getRelationColumns($table, $relations, $withRelations));
|
||||
|
||||
$this->leftJoinRelations($select, $table, $relations, $withRelations);
|
||||
$select->columns($columns);
|
||||
@@ -201,21 +159,22 @@ class PersistentDataManager implements IPersistentDataManager
|
||||
return $select;
|
||||
}
|
||||
|
||||
private function getRelationColumns(array $relations, array $withRelations): array
|
||||
private function getRelationColumns(string $table, array $relations, array $withRelations): array
|
||||
{
|
||||
$columns = [];
|
||||
|
||||
foreach ($relations as $relation => $relationType) {
|
||||
$relationTableAlias = $table . '__' . $relation;
|
||||
$relationTable = call_user_func([$relationType, 'getTable']);
|
||||
foreach (call_user_func([$relationType, 'getFields']) as $relationField) {
|
||||
$columns[] = [$relationTable, $relationField, $relation . '__' . $relationField];
|
||||
$columns[] = [$relationTableAlias, $relationField, $relation . '__' . $relationField];
|
||||
}
|
||||
|
||||
$nextOrderRelations = call_user_func([$relationType, 'getRelations']);
|
||||
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
||||
if (count($withRelations)) {
|
||||
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
|
||||
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
|
||||
}
|
||||
$columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations, $withRelations));
|
||||
$columns = array_merge($columns, $this->getRelationColumns($relationTable, $relationsOfRelation, $withRelations));
|
||||
}
|
||||
|
||||
return $columns;
|
||||
@@ -224,17 +183,70 @@ class PersistentDataManager implements IPersistentDataManager
|
||||
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void
|
||||
{
|
||||
foreach ($relations as $relation => $relationType) {
|
||||
$relationTableAlias = $table . '__' . $relation;
|
||||
$relationTable = call_user_func([$relationType, 'getTable']);
|
||||
$select->leftJoin($relationTable, [$relationTable, 'id'], '=', [$table, $relation . '_id']);
|
||||
$select->setTableAliases([$relationTableAlias => $relationTable]);
|
||||
$select->leftJoin($relationTableAlias, [$relationTableAlias, 'id'], '=', [$table, $relation . '_id']);
|
||||
|
||||
$nextOrderRelations = call_user_func([$relationType, 'getRelations']);
|
||||
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
||||
if (count($withRelations)) {
|
||||
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
|
||||
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
|
||||
}
|
||||
$this->leftJoinRelations($select, $relationTable, $nextOrderRelations, $withRelations);
|
||||
$this->leftJoinRelations($select, $relationTable, $relationsOfRelation, $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 . '__') {
|
||||
if ($data[$relation . '__id'] !== null) {
|
||||
$relationType = current($relations);
|
||||
$relationModel = new $relationType();
|
||||
$this->fillWithData($data, $relationModel, $withRelations, $relation);
|
||||
|
||||
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
|
||||
$model->$method($relationModel);
|
||||
} else {
|
||||
next($data);
|
||||
}
|
||||
|
||||
next($relations);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$model->saveSnapshot();
|
||||
}
|
||||
|
||||
private function syncRelations(Model $model): void
|
||||
{
|
||||
foreach ($model::getRelations() as $relation => $relationType) {
|
||||
|
||||
Reference in new issue
Block a user