User Interface
Interactivity
Business Model
Persistence

One major benefit of Server Side Rendered applications is ability to directly interact with data. In other applications you may need to manually process data but in Agile Toolkit we use data mapping framework.

/* Showing Class definition.
class DemoInvoice extends \Atk4\Data\Model
{
    public $title_field = 'reference';

    protected function init(): void
    {
        parent::init();

        $this->addField('reference');
        $this->addField('date', ['type' => 'date']);
    }
}
*/
session_start();

$model = new \Atk4\Ui\Demos\DemoInvoice(new \Atk4\Data\Persistence\Array_($_SESSION['x'] ?? []), ['dateFormat' => $owner->getApp()->ui_persistence->date_format]);
$model->onHook(\Atk4\Data\Model::HOOK_AFTER_SAVE, function ($model) {
    $_SESSION['x'][$model->getId()] = $model->get();
});

Header::addTo($owner, ['Set invoice data:']);
$form = \Atk4\Ui\Form::addTo($owner);
$form->setModel($model)->tryLoad(1);

if (!$model->loaded()) {
    // set default data
    $model->setMulti([
        'id' => 1,
        'reference' => 'Inv-' . random_int(1000, 9999),
        'date' => date($owner->getApp()->ui_persistence->date_format),
    ]);
    $model->save();
}

$form->onSubmit(function ($f) {
    $f->model->save();

    return new JsToast('Saved!');
});

\Atk4\Ui\View::addTo($owner, ['ui' => 'divider']);
Set invoice data:
Save

This code shows you a combination of 3 objects:

  • Form - a generic view that can display and handle any form
  • Model - defines fields for a business object
  • Persistence - creates a persistent storage location for the data

All three are combined by "setModel()" function and that is consistent throughout all the views.