Download the PHP package cycle/transaction without Composer
On this page you can find all versions of the php package cycle/transaction. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cycle/transaction
More information about cycle/transaction
Files in cycle/transaction
Package transaction
Short Description Transaction abstraction for Cycle ORM: run DBAL operations and a scoped Entity Manager within a single database transaction.
License BSD-3-Clause
Informations about the package transaction
Transaction Abstraction for Cycle ORM
This library provides a small transaction abstraction for Cycle ORM. It opens a single database transaction and lets you run raw DBAL operations together with a scoped Entity Manager inside it.
Everything executed within the callback shares one transaction: if the callback throws, the transaction is rolled back; otherwise it is committed. The Entity Manager is scoped to a single database connection for the duration of the callback, so accidental cross-connection writes are rejected instead of silently splitting your work across transactions.
Installation
The preferred way to install this package is through Composer.
Usage
Obtaining a Transaction
The package ships a single implementation of the Cycle\Transaction\Transaction interface. Build it
from your configured ORM and DBAL, or resolve Cycle\Transaction\Transaction from your framework's
container if it is registered there.
Basic Example
The callback receives the scoped EntityManagerInterface and the DatabaseInterface of the resolved
connection. Persisted entities are flushed and committed automatically when the callback returns.
If the callback throws, the whole transaction — both the Entity Manager changes and the raw DBAL operations — is rolled back.
Returning a Value
transact() returns whatever the callback returns.
Flush Modes
The FlushMode enum controls when and how the scoped Entity Manager flushes its pending changes.
Pass it via the $flush argument (defaults to FlushMode::BeforeCommit).
| Mode | Behaviour |
|---|---|
FlushMode::OnWrite |
Flush every persist/persistState/delete to the database immediately. |
FlushMode::BeforeCommit |
Collect all changes and flush them once, right before the transaction is committed. (default) |
FlushMode::FailOnPending |
Do not flush automatically; throw a TransactionException if any changes are still pending. |
FlushMode::SkipPending |
Do not flush automatically; silently discard any pending changes (only DBAL operations are committed). |
Transaction Modes
The TransactionMode enum controls how the Entity Manager's Unit of Work interacts with the open
transaction. Pass it via the $emMode argument (defaults to TransactionMode::Current).
| Mode | Behaviour |
|---|---|
TransactionMode::Current |
Reuse the currently opened transaction. Throws if none is open. (default) |
TransactionMode::OpenNew |
Open a new inner transaction per driver connection and close it on finish. |
TransactionMode::Ignore |
Do not manage transactions for the Unit of Work. |
TransactionMode::Exclusive |
Like OpenNew, but require the transaction to be top-level. Throws if wrapped by another. |
Selecting the Database
By default the transaction runs against the default database connection. Use the $source argument
to pick another connection — either by its name or by an entity class mapped to it. The scoped Entity
Manager then rejects entities that belong to a different connection.