Compare commits
3 Commits
9e3e2f96b2
...
373fccaa91
Author | SHA1 | Date | |
---|---|---|---|
373fccaa91 | |||
b47ef720d5 | |||
5599519f0f |
@ -50,6 +50,8 @@ class Connection implements IConnection
|
|||||||
{
|
{
|
||||||
$result = $this->connection->query($query);
|
$result = $this->connection->query($query);
|
||||||
|
|
||||||
|
print($query);
|
||||||
|
|
||||||
if ($result !== true) {
|
if ($result !== true) {
|
||||||
return new ResultSet($result);
|
return new ResultSet($result);
|
||||||
}
|
}
|
||||||
@ -79,6 +81,8 @@ class Connection implements IConnection
|
|||||||
{
|
{
|
||||||
$stmt = $this->connection->prepare($query);
|
$stmt = $this->connection->prepare($query);
|
||||||
|
|
||||||
|
print($query);
|
||||||
|
|
||||||
return new Statement($stmt);
|
return new Statement($stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,10 +148,9 @@ class PersistentDataManager implements IPersistentDataManager
|
|||||||
$relations = array_intersect_key($relations, array_flip($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));
|
||||||
|
|
||||||
$alreadyUsedTableAliases = [$table => true];
|
$this->leftJoinRelations($select, $table, $relations, $withRelations);
|
||||||
$this->leftJoinRelations($select, $table, $relations, $withRelations, $alreadyUsedTableAliases);
|
|
||||||
$select->columns($columns);
|
$select->columns($columns);
|
||||||
} else {
|
} else {
|
||||||
$select->columns($columns);
|
$select->columns($columns);
|
||||||
@ -160,59 +159,43 @@ class PersistentDataManager implements IPersistentDataManager
|
|||||||
return $select;
|
return $select;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getRelationColumns(array $relations, array $withRelations): array
|
private function getRelationColumns(string $table, array $relations, array $withRelations): array
|
||||||
{
|
{
|
||||||
$columns = [];
|
$columns = [];
|
||||||
|
|
||||||
foreach ($relations as $relation => $relationType) {
|
foreach ($relations as $relation => $relationType) {
|
||||||
|
$relationTableAlias = $table . '__' . $relation;
|
||||||
$relationTable = call_user_func([$relationType, 'getTable']);
|
$relationTable = call_user_func([$relationType, 'getTable']);
|
||||||
foreach (call_user_func([$relationType, 'getFields']) as $relationField) {
|
foreach (call_user_func([$relationType, 'getFields']) as $relationField) {
|
||||||
$columns[] = [$relationTable, $relationField, $relation . '__' . $relationField];
|
$columns[] = [$relationTableAlias, $relationField, $relation . '__' . $relationField];
|
||||||
}
|
}
|
||||||
|
|
||||||
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
||||||
if (count($withRelations)) {
|
if (count($withRelations)) {
|
||||||
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
|
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
|
||||||
}
|
}
|
||||||
$columns = array_merge($columns, $this->getRelationColumns($relationsOfRelation, $withRelations));
|
$columns = array_merge($columns, $this->getRelationColumns($relationTable, $relationsOfRelation, $withRelations));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $columns;
|
return $columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations, array &$alreadyUsedTableAliases): void
|
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void
|
||||||
{
|
{
|
||||||
foreach ($relations as $relation => $relationType) {
|
foreach ($relations as $relation => $relationType) {
|
||||||
$relationTable = $this->generateTableAlias($select, call_user_func([$relationType, 'getTable']), $alreadyUsedTableAliases);
|
$relationTableAlias = $table . '__' . $relation;
|
||||||
$select->leftJoin($relationTable, [$relationTable, 'id'], '=', [$table, $relation . '_id']);
|
$relationTable = call_user_func([$relationType, 'getTable']);
|
||||||
|
$select->setTableAliases([$relationTableAlias => $relationTable]);
|
||||||
|
$select->leftJoin($relationTableAlias, [$relationTableAlias, 'id'], '=', [$table, $relation . '_id']);
|
||||||
|
|
||||||
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
||||||
if (count($withRelations)) {
|
if (count($withRelations)) {
|
||||||
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
|
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
|
||||||
}
|
}
|
||||||
$this->leftJoinRelations($select, $relationTable, $relationsOfRelation, $withRelations, $alreadyUsedTableAliases);
|
$this->leftJoinRelations($select, $relationTable, $relationsOfRelation, $withRelations);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function generateTableAlias(Select $select, string $table, array &$alreadyUsedTableAliases)
|
|
||||||
{
|
|
||||||
if (!isset($alreadyUsedTableAliases[$table])) {
|
|
||||||
$alreadyUsedTableAliases[$table] = true;
|
|
||||||
return $table;
|
|
||||||
}
|
|
||||||
|
|
||||||
$i = 2;
|
|
||||||
do {
|
|
||||||
$alias = $table[0] . $i;
|
|
||||||
$i++;
|
|
||||||
} while (isset($alreadyUsedTableAliases[$alias]));
|
|
||||||
|
|
||||||
$select->setTableAliases([$alias => $table]);
|
|
||||||
$alreadyUsedTableAliases[$alias] = true;
|
|
||||||
|
|
||||||
return $alias;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
|
private function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
|
||||||
{
|
{
|
||||||
$relations = $model::getRelations();
|
$relations = $model::getRelations();
|
||||||
@ -244,12 +227,16 @@ class PersistentDataManager implements IPersistentDataManager
|
|||||||
|
|
||||||
next($data);
|
next($data);
|
||||||
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
|
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
|
||||||
$relationType = current($relations);
|
if ($data[$relation . '__id'] !== null) {
|
||||||
$relationModel = new $relationType();
|
$relationType = current($relations);
|
||||||
$this->fillWithData($data, $relationModel, $withRelations, $relation);
|
$relationModel = new $relationType();
|
||||||
|
$this->fillWithData($data, $relationModel, $withRelations, $relation);
|
||||||
|
|
||||||
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
|
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
|
||||||
$model->$method($relationModel);
|
$model->$method($relationModel);
|
||||||
|
} else {
|
||||||
|
next($data);
|
||||||
|
}
|
||||||
|
|
||||||
next($relations);
|
next($relations);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user