From 602d9063771e3c04550a07c917d12d31d8c9db89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Thu, 6 Mar 2025 12:30:36 +0100 Subject: [PATCH] refactor - do not assign model to "$model" --- README.md | 4 +-- demos/basic/breadcrumb.php | 6 ++--- demos/collection/multitable.php | 4 +-- demos/form/form-section.php | 37 ++++++++++++++-------------- demos/tutorial/intro.php | 6 ++--- docs/grid.md | 2 +- src/Form/Control/DropdownCascade.php | 4 ++- src/Form/Control/Lookup.php | 4 +-- 8 files changed, 34 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index f0743de716..ecc70bc0d9 100644 --- a/README.md +++ b/README.md @@ -161,9 +161,9 @@ $tabs->addTab('Settings', function (\Atk4\Ui\VirtualPage $p) use ($app) { // second tab contains an AJAX form that stores itself back to DB $m = new Settings($app->db); - $m = $m->load(2); + $entity = $m->load(2); \Atk4\Ui\Form::addTo($p) - ->setEntity($m); + ->setEntity($entity); }); ``` diff --git a/demos/basic/breadcrumb.php b/demos/basic/breadcrumb.php index f54504eb3c..49f394976a 100644 --- a/demos/basic/breadcrumb.php +++ b/demos/basic/breadcrumb.php @@ -29,13 +29,13 @@ $id = $app->uiPersistence->typecastAttributeLoadField($model->getIdField(), $crumb->stickyGet('country_id')); if ($id !== null) { // perhaps we edit individual country? - $model = $model->load($id); - $crumb->addCrumb($model->name, []); + $entity = $model->load($id); + $crumb->addCrumb($entity->name, []); // here we can check for additional criteria and display a deeper level on the crumb $form = Form::addTo($app); - $form->setEntity($model); + $form->setEntity($entity); $form->onSubmit(static function (Form $form) { return new JsToast('Form Submitted! Data saving is not possible in demo!'); }); diff --git a/demos/collection/multitable.php b/demos/collection/multitable.php index 8cd1a569e2..0db8d4503c 100644 --- a/demos/collection/multitable.php +++ b/demos/collection/multitable.php @@ -70,14 +70,14 @@ public function setModel(Model $model, array $route = []): void $path[] = $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getIdField(), $id); $pushModel = new $model($model->getPersistence()); - $pushModel = $pushModel->load($id); + $pushEntity = $pushModel->load($id); $ref = array_shift($route); if ($route === []) { $route[] = $ref; // repeat last route } - $pushModel = $pushModel->ref($ref); + $pushModel = $pushEntity->ref($ref); $pushModel->setOrder([File::hinting()->fieldName()->is_folder => 'desc', File::hinting()->fieldName()->name]); $table = Table::addTo($this->addColumn(), ['header' => false, 'class.very basic selectable' => true])->setStyle('cursor', 'pointer'); diff --git a/demos/form/form-section.php b/demos/form/form-section.php index 0e70d4a045..59fa54e368 100644 --- a/demos/form/form-section.php +++ b/demos/form/form-section.php @@ -18,8 +18,7 @@ ->link(['form-section-accordion']); View::addTo($app, ['ui' => 'clearing divider']); -$model = new Country($app->db); -$model = $model->loadAny(); +$entity = (new Country($app->db))->loadAny(); $saveAndDumpValues = static function (Form $form) { $form->entity->save(); @@ -35,22 +34,22 @@ // ----------------------------------------------------------------------------- $form = Form::addTo($app); -$form->setEntity($model, []); +$form->setEntity($entity, []); $sublayout = $form->layout->addSubLayout([Form\Layout\Section::class]); Header::addTo($sublayout, ['Column Section in Form']); -$sublayout->setEntity($model, [$model->fieldName()->name]); +$sublayout->setEntity($entity, [$entity->fieldName()->name]); $colsLayout = $form->layout->addSubLayout([Form\Layout\Section\Columns::class]); $c1 = $colsLayout->addColumn(); -$c1->setEntity($model, [$model->fieldName()->iso, $model->fieldName()->iso3]); +$c1->setEntity($entity, [$entity->fieldName()->iso, $entity->fieldName()->iso3]); $c2 = $colsLayout->addColumn(); -$c2->setEntity($model, [$model->fieldName()->numcode/* , $model->fieldName()->phonecode */]); +$c2->setEntity($entity, [$entity->fieldName()->numcode/* , $model->fieldName()->phonecode */]); -$form->addControl($model->fieldName()->phonecode); +$form->addControl($entity->fieldName()->phonecode); $form->onSubmit($saveAndDumpValues); @@ -59,20 +58,20 @@ // ----------------------------------------------------------------------------- $form = Form::addTo($app); -$form->setEntity($model, []); +$form->setEntity($entity, []); $sublayout = $form->layout->addSubLayout([Form\Layout\Section::class]); Header::addTo($sublayout, ['Accordion Section in Form']); -$sublayout->setEntity($model, [$model->fieldName()->name]); +$sublayout->setEntity($entity, [$entity->fieldName()->name]); $accordionLayout = $form->layout->addSubLayout([Form\Layout\Section\Accordion::class]); $a1 = $accordionLayout->addSection('Section 1'); -$a1->setEntity($model, [$model->fieldName()->iso, $model->fieldName()->iso3]); +$a1->setEntity($entity, [$entity->fieldName()->iso, $entity->fieldName()->iso3]); $a2 = $accordionLayout->addSection('Section 2'); -$a2->setEntity($model, [$model->fieldName()->numcode, $model->fieldName()->phonecode]); +$a2->setEntity($entity, [$entity->fieldName()->numcode, $entity->fieldName()->phonecode]); $form->onSubmit($saveAndDumpValues); @@ -81,20 +80,20 @@ // ----------------------------------------------------------------------------- $form = Form::addTo($app); -$form->setEntity($model, []); +$form->setEntity($entity, []); $sublayout = $form->layout->addSubLayout([Form\Layout\Section::class]); Header::addTo($sublayout, ['Tabs in Form']); -$sublayout->setEntity($model, [$model->fieldName()->name]); +$sublayout->setEntity($entity, [$entity->fieldName()->name]); $tabsLayout = $form->layout->addSubLayout([Form\Layout\Section\Tabs::class]); $tab1 = $tabsLayout->addTab('Tab 1'); -$tab1->addGroup('In Group')->setEntity($model, [$model->fieldName()->iso, $model->fieldName()->iso3]); +$tab1->addGroup('In Group')->setEntity($entity, [$entity->fieldName()->iso, $entity->fieldName()->iso3]); $tab2 = $tabsLayout->addTab('Tab 2'); -$tab2->setEntity($model, [$model->fieldName()->numcode, $model->fieldName()->phonecode]); +$tab2->setEntity($entity, [$entity->fieldName()->numcode, $entity->fieldName()->phonecode]); $form->onSubmit($saveAndDumpValues); @@ -105,21 +104,21 @@ Header::addTo($app, ['Color in form']); $form = Form::addTo($app); -$form->setEntity($model, []); +$form->setEntity($entity, []); $sublayout = $form->layout->addSubLayout([Form\Layout\Section::class, 'ui' => 'segment red inverted'], false); Header::addTo($sublayout, ['This section in Red', 'ui' => 'dividing header', 'element' => 'h2']); -$sublayout->setEntity($model, [$model->fieldName()->name]); +$sublayout->setEntity($entity, [$entity->fieldName()->name]); $sublayout = $form->layout->addSubLayout([Form\Layout\Section::class, 'ui' => 'segment teal inverted']); $colsLayout = $sublayout->addSubLayout([Form\Layout\Section\Columns::class]); $c1 = $colsLayout->addColumn(); -$c1->setEntity($model, [$model->fieldName()->iso, $model->fieldName()->iso3]); +$c1->setEntity($entity, [$entity->fieldName()->iso, $entity->fieldName()->iso3]); $c2 = $colsLayout->addColumn(); -$c2->setEntity($model, [$model->fieldName()->numcode, $model->fieldName()->phonecode]); +$c2->setEntity($entity, [$entity->fieldName()->numcode, $entity->fieldName()->phonecode]); $form->onSubmit($saveAndDumpValues); diff --git a/demos/tutorial/intro.php b/demos/tutorial/intro.php index 39c17578a4..16c1c449d3 100644 --- a/demos/tutorial/intro.php +++ b/demos/tutorial/intro.php @@ -193,10 +193,10 @@ protected function init(): void }); Header::addTo($owner, ['Record display in Card View using model data.']); - $model = $model->tryLoad(1); - if ($model !== null) { + $entity = $model->tryLoad(1); + if ($entity !== null) { Card::addTo($owner, ['useLabel' => true]) - ->setEntity($model); + ->setEntity($entity); } else { Message::addTo($owner, ['Empty record.']); } diff --git a/docs/grid.md b/docs/grid.md index 7e541ba49e..45cb7eeb7c 100644 --- a/docs/grid.md +++ b/docs/grid.md @@ -152,7 +152,7 @@ to populate a content: ``` $grid->addModalAction('Details', 'Additional Details', function (View $p, $id) use ($grid) { // $id of the record which was clicked - // $model = $grid->model->load($id); + // $entity = $grid->model->load($id); LoremIpsum::addTo($p); }); diff --git a/src/Form/Control/DropdownCascade.php b/src/Form/Control/DropdownCascade.php index 6a9455a7aa..c758fb7a94 100644 --- a/src/Form/Control/DropdownCascade.php +++ b/src/Form/Control/DropdownCascade.php @@ -35,7 +35,9 @@ protected function init(): void ? $this->getApp()->uiPersistence->typecastAttributeLoadField($this->cascadeFrom->entityField->getField(), $this->getApp()->getRequestPostParam($this->cascadeFrom->name)) : $this->cascadeFrom->entityField->get(); - $this->model = $this->cascadeFrom->model ? $this->cascadeFrom->model->ref($this->reference) : null; + $this->model = $this->cascadeFrom->model + ? $this->cascadeFrom->model->ref($this->reference) + : null; // populate default dropdown values $this->dropdownOptions['values'] = $this->getJsValues($this->getNewValues($cascadeFromValue), $this->entityField->get()); diff --git a/src/Form/Control/Lookup.php b/src/Form/Control/Lookup.php index 327a7b8fdd..fdfcd45c86 100644 --- a/src/Form/Control/Lookup.php +++ b/src/Form/Control/Lookup.php @@ -397,9 +397,9 @@ protected function renderView(): void $idField = $this->idField ?? $this->model->idField; - $this->model = $this->model->loadBy($idField, $this->entityField->get()); + $entity = $this->model->loadBy($idField, $this->entityField->get()); - $row = $this->renderRow($this->model); + $row = $this->renderRow($entity); $jsChain->dropdown('set value', $row['value'], true)->dropdown('set text', $row['title'], true); }