diff --git a/demos/form-control/multiline.php b/demos/form-control/multiline.php index 399cb50ab0..d5a605c9f1 100644 --- a/demos/form-control/multiline.php +++ b/demos/form-control/multiline.php @@ -60,7 +60,7 @@ protected function init(): void ]); $this->addExpression('total', [ 'expr' => function (Model $row) { - return $row->get('qty') * $row->get('box'); + return $row->expr('[qty] * [box]'); // @phpstan-ignore-line }, 'type' => 'integer', 'ui' => ['multiline' => [Form\Control\Multiline::TABLE_CELL => ['width' => 1, 'class' => 'blue']]], @@ -68,11 +68,20 @@ protected function init(): void } }); -$inventory = new $inventoryItemClass(new Persistence\Array_(), ['countryPersistence' => $app->db]); +$inventory = new $inventoryItemClass($app->db, ['table' => 'atk4_ui_multiline_test', 'countryPersistence' => $app->db]); + +$migrator = new \Atk4\Data\Schema\Migrator($inventory); +if (!$migrator->isTableExists($inventory->table)) { + $migrator->create(); +} // Populate some data. $total = 0; for ($i = 1; $i < 3; ++$i) { + if ($inventory->tryLoad($i) !== null) { + continue; + } + $entity = $inventory->createEntity(); $entity->set('id', $i); $entity->set('inv_date', new \DateTime()); diff --git a/src/Form/Control/Multiline.php b/src/Form/Control/Multiline.php index 4566b163c9..5aa6979be2 100644 --- a/src/Form/Control/Multiline.php +++ b/src/Form/Control/Multiline.php @@ -763,22 +763,27 @@ private function setDummyModelValue(Model $model): Model */ private function getExpressionValues(Model $model): array { - $dummyFields = []; + $dummyFields = $this->getExpressionFields($model); $formatValues = []; - foreach ($this->getExpressionFields($model) as $k => $field) { + foreach ($dummyFields as $k => $field) { if (!is_callable($field->expr)) { - $dummyFields[$k]['name'] = $field->shortName; - $dummyFields[$k]['expr'] = $this->getDummyExpression($field, $model); + $dummyFields[$k]->shortName = $field->shortName; // copy name to the same object? + $dummyFields[$k]->expr = $this->getDummyExpression($field, $model); } } if ($dummyFields !== []) { $dummyModel = new Model($model->getPersistence(), ['table' => $model->table]); + foreach ($model->getFields() as $field) { + if ($field->shortName !== 'id' && !isset($dummyFields[$field->shortName])) { + $dummyModel->addField($field->shortName, ['type' => $field->type]); + } + } foreach ($dummyFields as $field) { - $dummyModel->addExpression($field['name'], ['expr' => $field['expr'], 'type' => $model->getField($field['name'])->type]); + $dummyModel->addExpression($field->shortName, ['expr' => $field->expr, 'type' => $model->getField($field->shortName)->type]); } - $values = $dummyModel->loadAny()->get(); + $values = $dummyModel->loadOne()->get(); unset($values[$model->idField]); foreach ($values as $f => $value) { @@ -797,6 +802,8 @@ private function getExpressionValues(Model $model): array * * Get all field expression in model, but only evaluate expression used in * rowFields. + * + * @return array */ private function getExpressionFields(Model $model): array { @@ -806,7 +813,7 @@ private function getExpressionFields(Model $model): array continue; } - $fields[] = $field; + $fields[$field->shortName] = $field; } return $fields;