- Lean's API WIP on forms and ApiRenderer
This commit is contained in:
1 parent
3ce356e1f2
commit
4d7053a9cb
30 files changed
+1003
-56
No files matched your search
+12
-9
@@ -22,17 +22,17 @@ abstract class Save implements \Picea\Ui\Method\FormInterface {
|
||||
return $context->valid($this->getEntity()->isLoaded() ? $this->getEntity() : null);
|
||||
}
|
||||
|
||||
public function execute(FormContextInterface $context) : void
|
||||
public function execute(FormContextInterface $context) : mixed
|
||||
{
|
||||
$entity = $this->getEntity();
|
||||
|
||||
if ($entity->isLoaded() ) {
|
||||
if (property_exists($entity, 'updatedAt')) {
|
||||
if ($entity->isLoaded()) {
|
||||
if (property_exists($entity, 'updatedAt') && $entity->repository()->generateDatasetDiff($entity) ) {
|
||||
$entity->updatedAt = new Datetime();
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (property_exists($entity, 'createdAt')) {
|
||||
if (property_exists($entity, 'createdAt') && empty($entity->createdAt)) {
|
||||
$entity->createdAt = new Datetime();
|
||||
}
|
||||
}
|
||||
@@ -40,18 +40,22 @@ abstract class Save implements \Picea\Ui\Method\FormInterface {
|
||||
try {
|
||||
$this->assignContextToEntity($context);
|
||||
|
||||
if ( $entity::repository()->save($entity) ) {
|
||||
if ( $saved = $entity::repository()->save($entity) ) {
|
||||
$context->pushMessage($this->message::generateSuccess(
|
||||
$this->lang('lean.api.form.save.success.entity')
|
||||
));
|
||||
}
|
||||
else {
|
||||
throw new \InvalidArgumentException($this->lang('lean.api.form.save.error.entity'));
|
||||
$context->pushMessage($this->message::generateWarning(
|
||||
$this->lang('lean.api.form.save.error.entity', [ 'entity' => $this->entity::class ])
|
||||
));
|
||||
}
|
||||
}
|
||||
catch(\Throwable $ex) {
|
||||
throw new \ErrorException($this->lang('lean.api.form.save.error.pdo', [ 'error' => $ex->getMessage() ]));
|
||||
throw new \PDOException($this->lang('lean.api.form.save.error.pdo', [ 'error' => $ex->getMessage() ]));
|
||||
}
|
||||
|
||||
return $saved;
|
||||
}
|
||||
|
||||
protected function assignContextToEntity(FormContextInterface $context) : void
|
||||
@@ -60,13 +64,12 @@ abstract class Save implements \Picea\Ui\Method\FormInterface {
|
||||
|
||||
foreach($entity::resolveEntity()->fieldList() as $key => $property) {
|
||||
$field = $property->getAttribute(Field::class)->object;
|
||||
|
||||
if (! $field->readonly || ! $entity->isLoaded()) {
|
||||
$apiField = $property->getAttribute(EntityField::class)->object ?? null;
|
||||
|
||||
if ($apiField) {
|
||||
$var = $apiField->field ?: $key;
|
||||
|
||||
|
||||
if ( isset($context->{$var}) ) {
|
||||
if ($apiField->setterMethod) {
|
||||
# Use a setter method
|
||||
|
||||
Reference in new issue
Block a user