Skip to content

Commit

Permalink
dedup all join methods excl. afterLoad
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 16, 2022
1 parent 47e461a commit b363b30
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 159 deletions.
89 changes: 85 additions & 4 deletions src/Model/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
74 changes: 0 additions & 74 deletions src/Persistence/Array_/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
81 changes: 0 additions & 81 deletions src/Persistence/Sql/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
}
}

0 comments on commit b363b30

Please sign in to comment.