diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2020-10-05 21:44:27 +0200 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2020-10-05 21:44:27 +0200 |
commit | 01842f99b65b06d2647470c3b867719e72dabde7 (patch) | |
tree | 6ee373d5df46cf1097b06fa37bcfb0dc9b09d811 /ratatoeskr/sys/DbTransaction.php | |
parent | 5e347e4efaa81c2108256dc927208cd55dc10baa (diff) | |
download | ratatoeskr-cms-01842f99b65b06d2647470c3b867719e72dabde7.tar.gz ratatoeskr-cms-01842f99b65b06d2647470c3b867719e72dabde7.tar.bz2 ratatoeskr-cms-01842f99b65b06d2647470c3b867719e72dabde7.zip |
Introduce some new database wrappers
This will allow us to avoid using globals and use the autoloader in the
future.
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(); + } + } +} |