Skip to content

Commit fe1273c

Browse files
committed
WIP save using after_save hook
1 parent db8fbe7 commit fe1273c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Form/Control/Multiline.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,15 @@ public function setInputValue(string $value): void
351351
}
352352
}
353353

354+
$ourEntity = $this->entityField->getField()->hasReference()
355+
? $this->entityField->getEntity()
356+
: $this->form->entity;
357+
$theirReference = $this->entityField->getField()->hasReference()
358+
? $this->entityField->getField()->getReference()
359+
: new \Atk4\Data\Reference('x'); // TODO
360+
361+
$changes->saveOnSave($ourEntity, $theirReference);
362+
354363
$this->changes = $changes;
355364
}
356365

src/Form/Control/TheirChanges.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Atk4\Data\Exception;
88
use Atk4\Data\Model;
9+
use Atk4\Data\Reference;
910

1011
/**
1112
* TODO move to atk4/data and allow deep/nested changes.
@@ -74,4 +75,17 @@ public function saveTo(Model $theirModelOrEntity): void
7475
}
7576
});
7677
}
78+
79+
public function saveOnSave(Model $ourModelOrEntity, Reference $theirReference): void
80+
{
81+
$theirReference->assertOurModelOrEntity($ourModelOrEntity);
82+
83+
$hookIndex = $ourModelOrEntity->onHook(Model::HOOK_AFTER_SAVE, function (Model $m) use ($ourModelOrEntity, $theirReference, &$hookIndex) {
84+
assert($m === $ourModelOrEntity); // TODO allow cloning?
85+
86+
$ourModelOrEntity->removeHook(Model::HOOK_AFTER_SAVE, $hookIndex, true);
87+
88+
$this->saveTo($theirReference->ref($m));
89+
});
90+
}
7791
}

0 commit comments

Comments
 (0)