uuid = Uuid::v4()->toString(); } /** * Get UUID of the transaction. * @return string */ public function getUuid(): string { return $this->uuid; } /** * Get the parent transaction. * @return Transaction|null */ public function getParent(): ?Transaction { return $this->parentTransaction; } /** * Set the current child transaction of this transaction. * @param Transaction|null $transaction The child transaction. NULL if there is no child transaction. * @return void */ public function setChild(?Transaction $transaction): void { $this->childTransaction = $transaction; } /** * Start the transaction. * @return void * @throws NotCurrentTransactionException */ public function start(): void { // Transaction activation state changed. $this->active = true; $this->database->onTransactionStateChanged($this); // Start the transaction. $this->database->getQueriesAdapter()->newTransaction(); } /** * Commit the transaction. * @return void * @throws NotCurrentTransactionException */ public function commit(): void { if (!$this->isActive()) // Do not commit a non-active transaction. return; if (!empty($this->childTransaction)) // First commit the child transaction, if there is one. $this->childTransaction->commit(); // Transaction activation state changed. $this->active = false; $this->database->onTransactionStateChanged($this); // Commit the current transaction. $this->database->getQueriesAdapter()->commitTransaction(); } /** * Rollback the transaction. * @return void * @throws NotCurrentTransactionException */ public function rollback(): void { if (!$this->isActive()) // Do not rollback a non-active transaction. return; if (!empty($this->childTransaction)) // First rollback the child transaction, if there is one. $this->childTransaction->rollback(); // Transaction activation state changed. $this->active = false; $this->database->onTransactionStateChanged($this); // Rollback the current transaction. $this->database->getQueriesAdapter()->rollbackTransaction(); } /** * Determine if the transaction is still active (not committed nor rolled back). * @return bool */ public function isActive(): bool { return $this->active; } }