Skip to content

Commit

Permalink
refactor - do not assign model to "$model"
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Mar 6, 2025
1 parent ebf03cc commit 602d906
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
```

Expand Down
6 changes: 3 additions & 3 deletions demos/basic/breadcrumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});
Expand Down
4 changes: 2 additions & 2 deletions demos/collection/multitable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
37 changes: 18 additions & 19 deletions demos/form/form-section.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions demos/tutorial/intro.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.']);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
4 changes: 3 additions & 1 deletion src/Form/Control/DropdownCascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Control/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 602d906

Please sign in to comment.