Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix demos readonly rollback with nested transactions #2116

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading