Compare commits
No commits in common. "4283bc9bb15d17914393b4ba3463d83717487c53" and "8d490e48aaeb7ad2843e402fa42ec266db52e809" have entirely different histories.
4283bc9bb1
...
8d490e48aa
@ -16,8 +16,6 @@ interface IPersistentDataManager
|
||||
|
||||
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void;
|
||||
|
||||
public function loadMultiRelationsFromDb(array $models, string $relation, bool $useRelations = false, array $withRelations = []): void;
|
||||
|
||||
public function saveToDb(Model $model): void;
|
||||
|
||||
public function deleteFromDb(Model $model): void;
|
||||
|
@ -8,8 +8,6 @@ abstract class Model
|
||||
|
||||
protected static array $relations = [];
|
||||
|
||||
protected static array $multiRelations = [];
|
||||
|
||||
protected $id = null;
|
||||
|
||||
private array $snapshot = [];
|
||||
@ -29,11 +27,6 @@ abstract class Model
|
||||
return static::$relations;
|
||||
}
|
||||
|
||||
public static function getMultiRelations(): array
|
||||
{
|
||||
return static::$multiRelations;
|
||||
}
|
||||
|
||||
public function setId($id): void
|
||||
{
|
||||
$this->id = $id;
|
||||
|
@ -92,40 +92,6 @@ class PersistentDataManager implements IPersistentDataManager
|
||||
}
|
||||
}
|
||||
|
||||
public function loadMultiRelationsFromDb(array $models, string $relation, bool $useRelations = false, array $withRelations = []): void
|
||||
{
|
||||
if (count($models) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$parentModelType = get_class($models[0]);
|
||||
$relationModelsSetter = 'set' . str_replace('_', '', ucwords($relation, '_'));
|
||||
[$relationModelType, $reverseRelation] = call_user_func([$parentModelType, 'getMultiRelations'])[$relation];
|
||||
$reverseRelationIdGetter = 'get' . str_replace('_', '', ucwords($reverseRelation, '_')) . 'Id';
|
||||
|
||||
$parentModelsById = [];
|
||||
foreach ($models as $model) {
|
||||
$parentModelsById[$model->getId()] = $model;
|
||||
}
|
||||
|
||||
$select = new Select($this->dbConnection);
|
||||
$select->where($reverseRelation . '_id', 'IN', array_keys($parentModelsById));
|
||||
|
||||
$relationsByParentModelId = [];
|
||||
foreach ($this->selectMultipleFromDb($select, $relationModelType, $useRelations, $withRelations) as $relationModel) {
|
||||
$reverseRelationId = $relationModel->$reverseRelationIdGetter();
|
||||
if (!isset($relationsByParentModelId[$reverseRelationId])) {
|
||||
$relationsByParentModelId[$reverseRelationId] = [];
|
||||
}
|
||||
$relationsByParentModelId[$reverseRelationId][] = $relationModel;
|
||||
}
|
||||
|
||||
foreach ($parentModelsById as $parentModelId => $parentModel) {
|
||||
$relationModels = $relationsByParentModelId[$parentModelId] ?? [];
|
||||
$parentModel->$relationModelsSetter($relationModels);
|
||||
}
|
||||
}
|
||||
|
||||
public function saveToDb(Model $model): void
|
||||
{
|
||||
$this->syncRelations($model);
|
||||
|
Loading…
Reference in New Issue
Block a user