Skip to content

Commit

Permalink
Fix demos readonly rollback with nested transactions (#2116)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Oct 3, 2023
1 parent 4a71a77 commit 1475c66
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
7 changes: 5 additions & 2 deletions demos/init-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ protected function isAllowDbModifications(): bool

public function atomic(\Closure $fx)
{
$eRollback = new \Exception('Prevent modification');
$connection = $this->getModel(true)->getPersistence()->getConnection(); // @phpstan-ignore-line
$eRollback = !$connection->inTransaction()
? new \Exception('Prevent modification')
: null; // TODO replace with atk4/data Connection before commit hook
$res = null;
try {
parent::atomic(function () use ($fx, $eRollback, &$res) {
$res = $fx();

if (!$this->isAllowDbModifications()) {
if ($eRollback !== null && !$this->isAllowDbModifications()) {
throw $eRollback;
}
});
Expand Down
6 changes: 0 additions & 6 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ parameters:
- '~^Only booleans are allowed in .+, .+ given( on the (left|right) side)?\.~'
- '~^Variable (static )?(property access|method call) on .+\.~'

# https://github.com/phpstan/phpstan/issues/9951
-
path: 'src/Table/Column/FilterModel/Type*.php'
message: '~^Parameter (#2 \$operator|#3 \$value) of method Atk4\\Data\\Model::addCondition\(\) expects (Atk4\\Data\\Persistence\\Sql\\Expressionable\|string, bool|string, DateTime) given\.$~'
count: 5

# TODO these rules are generated, this ignores should be fixed in the code
# for level = 2
-
Expand Down
11 changes: 8 additions & 3 deletions src/Form/Control/Multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,9 @@ public function saveRows(): self
$currentIds = array_column($model->export(), $model->idField);

foreach ($this->rowData as $row) {
$entity = $row[$model->idField] !== null ? $model->load($row[$model->idField]) : $model->createEntity();
$entity = $row[$model->idField] !== null
? $model->load($row[$model->idField])
: $model->createEntity();
foreach ($row as $fieldName => $value) {
if ($fieldName === '__atkml') {
continue;
Expand All @@ -350,9 +352,12 @@ public function saveRows(): self
$entity->set($fieldName, $value);
}
}
$id = $entity->save()->getId();

$k = array_search($id, $currentIds, true);
if (!$entity->isLoaded() || $entity->getDirtyRef() !== []) {
$entity->save();
}

$k = array_search($entity->getId(), $currentIds, true);
if ($k !== false) {
unset($currentIds[$k]);
}
Expand Down

0 comments on commit 1475c66

Please sign in to comment.