Skip to content

Commit 63416c6

Browse files
committed
Optimize save method
1 parent 0667a41 commit 63416c6

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

src/AbstractTable.php

+8-24
Original file line numberDiff line numberDiff line change
@@ -72,36 +72,20 @@ public function save(AbstractEntity &$entity): void
7272
$entity->updated_at = new \DateTimeImmutable();
7373
$changedColumns['updated_at'] = DateTimeHelper::dateTimeToString($entity->updated_at);
7474
}
75-
76-
7775
if ($this->config->hasOptimisticLock()
7876
&& method_exists($entity, 'getVersion')
7977
&& method_exists($entity, 'incrementVersion')) {
8078
$where['lock_version'] = $entity->getVersion();
8179
$entity->incrementVersion();
8280
$changedColumns['lock_version'] = $entity->getVersion();
83-
84-
try {
85-
$connection->beginTransaction();
86-
$versionUpdated = $connection->update(
87-
$this->getTableName(),
88-
$changedColumns,
89-
$where
90-
);
91-
if (!$versionUpdated) {
92-
throw new Exceptions\LockException('Failed to update entity version, concurrency modification, rolling back.');
93-
}
94-
$connection->commit();
95-
} catch (\Throwable $e) {
96-
$connection->rollBack();
97-
throw $e;
98-
}
99-
} else {
100-
$connection->update(
101-
$this->getTableName(),
102-
$changedColumns,
103-
$where
104-
);
81+
}
82+
$entityUpdated = $connection->update(
83+
table: $this->getTableName(),
84+
data: $changedColumns,
85+
criteria: $where,
86+
);
87+
if ($this->config->hasOptimisticLock() && !$entityUpdated) {
88+
throw new Exceptions\LockException('Failed to update entity version, concurrency modification, rolling back.');
10589
}
10690
$entity->resetChangedColumns();
10791
}

0 commit comments

Comments
 (0)