Skip to content

Commit

Permalink
DEBUG SQL persistence for multiline
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Aug 27, 2022
1 parent 56bb21c commit af050cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
13 changes: 11 additions & 2 deletions demos/form-control/multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,28 @@ 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']]],
]);
}
});

$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());
Expand Down
21 changes: 14 additions & 7 deletions src/Form/Control/Multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<string, SqlExpressionField>
*/
private function getExpressionFields(Model $model): array
{
Expand All @@ -806,7 +813,7 @@ private function getExpressionFields(Model $model): array
continue;
}

$fields[] = $field;
$fields[$field->shortName] = $field;
}

return $fields;
Expand Down

0 comments on commit af050cb

Please sign in to comment.