From b363b30ca7e2ea8e244ef12a5b056e59fe772d0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 16 Jan 2022 18:56:39 +0100 Subject: [PATCH] dedup all join methods excl. afterLoad --- src/Model/Join.php | 89 +++++++++++++++++++++++++++++++-- src/Persistence/Array_/Join.php | 74 --------------------------- src/Persistence/Sql/Join.php | 81 ------------------------------ 3 files changed, 85 insertions(+), 159 deletions(-) diff --git a/src/Model/Join.php b/src/Model/Join.php index 1b39c613c..5b1034bb8 100644 --- a/src/Model/Join.php +++ b/src/Model/Join.php @@ -514,11 +514,92 @@ protected function afterUnload(Model $entity): void abstract public function afterLoad(Model $entity): void; - abstract public function beforeInsert(Model $entity, array &$data): void; + public function beforeInsert(Model $entity, array &$data): void + { + if ($this->weak) { + return; + } + + $model = $this->getOwner(); + + // the value for the master_field is set, so we are going to use existing record anyway + if ($model->hasField($this->master_field) && $entity->get($this->master_field) !== null) { + return; + } + + $foreignModel = $this->getForeignModel(); + $foreignEntity = $foreignModel->createEntity() + ->setMulti($this->getAndUnsetSaveBuffer($entity)) + /*->set($this->foreign_field, null)*/; + $foreignEntity->save(); + + $this->setId($entity, $foreignEntity->getId()); + + if ($this->hasJoin()) { + $this->getJoin()->setSaveBufferValue($entity, $this->master_field, $this->getId($entity)); + } else { + $data[$this->master_field] = $this->getId($entity); + } + + // $entity->set($this->master_field, $this->getId($entity)); + } + + public function afterInsert(Model $entity): void + { + if ($this->weak) { + return; + } + + $this->setSaveBufferValue($entity, $this->foreign_field, $this->hasJoin() ? $this->getJoin()->getId($entity) : $entity->getId()); // from array persistence... - abstract public function afterInsert(Model $entity): void; + $foreignModel = $this->getForeignModel(); + $foreignEntity = $foreignModel->createEntity() + ->setMulti($this->getAndUnsetSaveBuffer($entity)) + ->set($this->foreign_field, $this->hasJoin() ? $this->getJoin()->getId($entity) : $entity->getId()); + $foreignEntity->save(); - abstract public function beforeUpdate(Model $entity, array &$data): void; + $this->setId($entity, $entity->getId()); // TODO why is this here? it seems to be not needed + } + + public function beforeUpdate(Model $entity, array &$data): void + { + if ($this->weak) { + return; + } + + if (!$this->issetSaveBuffer($entity)) { + return; + } + + $foreignModel = $this->getForeignModel(); + $foreignId = $this->reverse ? $entity->getId() : $entity->get($this->master_field); + $saveBuffer = $this->getAndUnsetSaveBuffer($entity); + $foreignModel->atomic(function () use ($foreignModel, $foreignId, $saveBuffer) { + $foreignModel = (clone $foreignModel)->addCondition($this->foreign_field, $foreignId); + foreach ($foreignModel as $foreignEntity) { + $foreignEntity->setMulti($saveBuffer); + $foreignEntity->save(); + } + }); - abstract public function doDelete(Model $entity): void; + // $this->setId($entity, ??); // TODO needed? from array persistence + } + + public function doDelete(Model $entity): void + { + if ($this->weak) { + return; + } + + $foreignModel = $this->getForeignModel(); + $foreignId = $this->reverse ? $entity->getId() : $entity->get($this->master_field); + $foreignModel->atomic(function () use ($foreignModel, $foreignId) { + $foreignModel = (clone $foreignModel)->addCondition($this->foreign_field, $foreignId); + foreach ($foreignModel as $foreignEntity) { + $foreignEntity->delete(); + } + }); + + $this->unsetId($entity); // TODO needed? from array persistence + } } diff --git a/src/Persistence/Array_/Join.php b/src/Persistence/Array_/Join.php index b39646c48..6861303b9 100644 --- a/src/Persistence/Array_/Join.php +++ b/src/Persistence/Array_/Join.php @@ -32,78 +32,4 @@ public function afterLoad(Model $entity): void $dataRef = &$entity->getDataRef(); $dataRef = array_merge($data, $entity->getDataRef()); } - - public function beforeInsert(Model $entity, array &$data): void - { - if ($this->weak) { - return; - } - - if ($entity->hasField($this->master_field) && $entity->get($this->master_field)) { - // The value for the master_field is set, - // we are going to use existing record. - return; - } - - // Figure out where are we going to save data - $persistence = $this->persistence ?: $this->getOwner()->persistence; - - $lastInsertedId = $persistence->insert( - $this->createFakeForeignModel(), - $this->getAndUnsetSaveBuffer($entity) - ); - $this->setId($entity, $lastInsertedId); - - $data[$this->master_field] = $this->getId($entity); - - // $entity->set($this->master_field, $this->getId($entity)); - } - - public function afterInsert(Model $entity): void - { - if ($this->weak) { - return; - } - - $this->setSaveBufferValue($entity, $this->foreign_field, $this->hasJoin() ? $this->getJoin()->getId($entity) : $entity->getId()); - - $persistence = $this->persistence ?: $this->getOwner()->persistence; - - $lastInsertedId = $persistence->insert( - $this->createFakeForeignModel(), - $this->getAndUnsetSaveBuffer($entity) - ); - $this->setId($entity, $lastInsertedId); - } - - public function beforeUpdate(Model $entity, array &$data): void - { - if ($this->weak) { - return; - } - - $persistence = $this->persistence ?: $this->getOwner()->persistence; - - $persistence->update( - $this->createFakeForeignModel(), - $this->getId($entity), - $this->getAndUnsetSaveBuffer($entity) - ); - // $this->setId($entity, ??); - } - - public function doDelete(Model $entity): void - { - if ($this->weak) { - return; - } - - $persistence = $this->persistence ?: $this->getOwner()->persistence; - - $persistence->delete( - $this->createFakeForeignModel(), - $this->getId($entity) - ); - $this->unsetId($entity); - } } diff --git a/src/Persistence/Sql/Join.php b/src/Persistence/Sql/Join.php index 670f79888..d41aecdd2 100644 --- a/src/Persistence/Sql/Join.php +++ b/src/Persistence/Sql/Join.php @@ -107,85 +107,4 @@ public function afterLoad(Model $entity): void unset($entity->getDataRef()[$this->short_name]); } } - - public function beforeInsert(Model $entity, array &$data): void - { - if ($this->weak) { - return; - } - - $model = $this->getOwner(); - - // the value for the master_field is set, so we are going to use existing record anyway - if ($model->hasField($this->master_field) && $entity->get($this->master_field) !== null) { - return; - } - - $foreignModel = $this->getForeignModel(); - $foreignEntity = $foreignModel->createEntity() - ->setMulti($this->getAndUnsetSaveBuffer($entity)) - /*->set($this->foreign_field, null)*/; - $foreignEntity->save(); - - $this->setId($entity, $foreignEntity->getId()); - - if ($this->hasJoin()) { - $this->getJoin()->setSaveBufferValue($entity, $this->master_field, $this->getId($entity)); - } else { - $data[$this->master_field] = $this->getId($entity); - } - } - - public function afterInsert(Model $entity): void - { - if ($this->weak) { - return; - } - - $foreignModel = $this->getForeignModel(); - $foreignEntity = $foreignModel->createEntity() - ->setMulti($this->getAndUnsetSaveBuffer($entity)) - ->set($this->foreign_field, $this->hasJoin() ? $this->getJoin()->getId($entity) : $entity->getId()); - $foreignEntity->save(); - - $this->setId($entity, $entity->getId()); // TODO why is this here? it seems to be not needed - } - - public function beforeUpdate(Model $entity, array &$data): void - { - if ($this->weak) { - return; - } - - if (!$this->issetSaveBuffer($entity)) { - return; - } - - $foreignModel = $this->getForeignModel(); - $foreignId = $this->reverse ? $entity->getId() : $entity->get($this->master_field); - $saveBuffer = $this->getAndUnsetSaveBuffer($entity); - $foreignModel->atomic(function () use ($foreignModel, $foreignId, $saveBuffer) { - $foreignModel = (clone $foreignModel)->addCondition($this->foreign_field, $foreignId); - foreach ($foreignModel as $foreignEntity) { - $foreignEntity->setMulti($saveBuffer); - $foreignEntity->save(); - } - }); - } - - public function doDelete(Model $entity): void - { - if ($this->weak) { - return; - } - - $foreignModel = $this->getForeignModel(); - $foreignId = $this->reverse ? $entity->getId() : $entity->get($this->master_field); - $foreignModel->atomic(function () use ($foreignModel, $foreignId) { - $foreignModel = (clone $foreignModel)->addCondition($this->foreign_field, $foreignId); - foreach ($foreignModel as $foreignEntity) { - $foreignEntity->delete(); - } - }); - } }