From 826484f65d33221e234e8adc9575463194188033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 26 Mar 2024 12:27:09 +0100 Subject: [PATCH] Introduce View::$entity property for entity access --- demos/form/form.php | 2 +- src/Form.php | 7 +++++-- src/Form/AbstractLayout.php | 6 +++--- src/View.php | 9 ++++++++- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/demos/form/form.php b/demos/form/form.php index ce2bf63809..fd43ad228c 100644 --- a/demos/form/form.php +++ b/demos/form/form.php @@ -34,7 +34,7 @@ $form->onSubmit(static function (Form $form) { // implement subscribe here - return $form->jsSuccess('Subscribed ' . $form->model->get('email') . ' to newsletter.'); + return $form->jsSuccess('Subscribed ' . $form->entity->get('email') . ' to newsletter.'); }); $form->buttonSave->set('Subscribe'); diff --git a/src/Form.php b/src/Form.php index ebb88da378..392a705e5c 100644 --- a/src/Form.php +++ b/src/Form.php @@ -20,6 +20,9 @@ use Atk4\Ui\Js\JsExpression; use Atk4\Ui\Js\JsExpressionable; +/** + * @property false $model use $entity property instead + */ class Form extends View { use HookTrait; @@ -359,7 +362,7 @@ public function addGroup($title = null) */ public function controlFactory(Field $field, $controlSeed = []): Control { - $this->model->assertIsEntity($field->getOwner()); + $this->entity->assertIsEntity($field->getOwner()); $fallbackSeed = [Control\Line::class]; @@ -394,7 +397,7 @@ public function controlFactory(Field $field, $controlSeed = []): Control $defaults = [ 'form' => $this, - 'entityField' => new EntityFieldPair($this->model, $field->shortName), + 'entityField' => new EntityFieldPair($this->entity, $field->shortName), 'shortName' => $field->shortName, ]; diff --git a/src/Form/AbstractLayout.php b/src/Form/AbstractLayout.php index 88cd94613f..c32b87672f 100644 --- a/src/Form/AbstractLayout.php +++ b/src/Form/AbstractLayout.php @@ -37,10 +37,10 @@ protected function _addControl(Control $control, Field $field): Control */ public function addControl(string $name, $control = [], array $fieldSeed = []): Control { - if ($this->form->model === null) { - $this->form->model = (new ProxyModel())->createEntity(); + if ($this->form->entity === null) { + $this->form->entity = (new ProxyModel())->createEntity(); } - $model = $this->form->model->getModel(); + $model = $this->form->entity->getModel(); // TODO this class should not refer to any specific form control $controlClass = is_object($control) diff --git a/src/View.php b/src/View.php index 1c6492351e..92da539db5 100644 --- a/src/View.php +++ b/src/View.php @@ -33,6 +33,7 @@ class View extends AbstractView protected array $_jsActions = []; public ?Model $model = null; + public ?Model $entity = null; /** * Name of the region in the parent's template where this object will output itself. @@ -118,7 +119,13 @@ public function setModel(Model $model): void throw new Exception('Different model is already set'); } - $this->model = $model; + if ($model->isEntity()) { + unset($this->{'model'}); + $this->entity = $model; + } else { + unset($this->{'entity'}); + $this->model = $model; + } } /**