diff options
Diffstat (limited to 'ratatoeskr/sys/DbTransaction.php')
-rw-r--r-- | ratatoeskr/sys/DbTransaction.php | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/ratatoeskr/sys/DbTransaction.php b/ratatoeskr/sys/DbTransaction.php new file mode 100644 index 0000000..ed1f9ac --- /dev/null +++ b/ratatoeskr/sys/DbTransaction.php @@ -0,0 +1,47 @@ +<?php + + +namespace r7r\cms\sys; + +class DbTransaction +{ + /** @var Database */ + private $db; + + /** @var bool */ + private $startedHere; + + /** + * Start a new transaction. + * @param Database $db + */ + public function __construct(Database $db) + { + $this->db = $db; + + $this->startedHere = !$this->db->getPdo()->inTransaction(); + if ($this->startedHere) { + $this->db->getPdo()->beginTransaction(); + } + } + + /** + * Commit the transaction. + */ + public function commit(): void + { + if ($this->startedHere) { + $this->db->getPdo()->commit(); + } + } + + /** + * Roll the transaction back. + */ + public function rollback(): void + { + if ($this->startedHere) { + $this->db->getPdo()->rollBack(); + } + } +} |