Model Transactions ================== When a process performs multiple database operations, it might be important that each step is completed successfully so that data integrity can be maintained. Transactions offer the ability to ensure that all database operations have been executed successfully before the data is committed to the database. Transactions in Phalcon allow you to commit all operations if they were executed successfully or rollback all operations if something went wrong. 手動のトランザクション ---------------------- If an application only uses one connection and the transactions aren't very complex, a transaction can be created by just moving the current connection into transaction mode and then commit or rollback the operation whether it is successful or not: .. code-block:: php db->begin(); $robot = new Robots(); $robot->name = "WALL·E"; $robot->created_at = date("Y-m-d"); // The model failed to save, so rollback the transaction if ($robot->save() === false) { $this->db->rollback(); return; } $robotPart = new RobotParts(); $robotPart->robots_id = $robot->id; $robotPart->type = "head"; // The model failed to save, so rollback the transaction if ($robotPart->save() === false) { $this->db->rollback(); return; } // Commit the transaction $this->db->commit(); } } 暗黙的なトランザクション ------------------------ Existing relationships can be used to store records and their related instances, this kind of operation implicitly creates a transaction to ensure that data is correctly stored: .. code-block:: php type = "head"; $robot = new Robots(); $robot->name = "WALL·E"; $robot->created_at = date("Y-m-d"); $robot->robotPart = $robotPart; // Creates an implicit transaction to store both records $robot->save(); Isolated Transactions --------------------- Isolated transactions are executed in a new connection ensuring that all the generated SQL, virtual foreign key checks and business rules are isolated from the main connection. This kind of transaction requires a transaction manager that globally manages each transaction created ensuring that they are correctly rolled back/committed before ending the request: .. code-block:: php get(); $robot = new Robots(); $robot->setTransaction($transaction); $robot->name = "WALL·E"; $robot->created_at = date("Y-m-d"); if ($robot->save() === false) { $transaction->rollback( "Cannot save robot" ); } $robotPart = new RobotParts(); $robotPart->setTransaction($transaction); $robotPart->robots_id = $robot->id; $robotPart->type = "head"; if ($robotPart->save() === false) { $transaction->rollback( "Cannot save robot part" ); } // Everything's gone fine, let's commit the transaction $transaction->commit(); } catch (TxFailed $e) { echo "Failed, reason: ", $e->getMessage(); } Transactions can be used to delete many records in a consistent way: .. code-block:: php get(); // Get the robots to be deleted $robots = Robots::find( "type = 'mechanical'" ); foreach ($robots as $robot) { $robot->setTransaction($transaction); // Something's gone wrong, we should rollback the transaction if ($robot->delete() === false) { $messages = $robot->getMessages(); foreach ($messages as $message) { $transaction->rollback( $message->getMessage() ); } } } // Everything's gone fine, let's commit the transaction $transaction->commit(); echo "Robots were deleted successfully!"; } catch (TxFailed $e) { echo "Failed, reason: ", $e->getMessage(); } Transactions are reused no matter where the transaction object is retrieved. A new transaction is generated only when a :code:`commit()` or :code:`rollback()` is performed. You can use the service container to create the global transaction manager for the entire application: .. code-block:: php setShared( "transactions", function () { return new TransactionManager(); } ); Then access it from a controller or view: .. code-block:: php di->getTransactions(); // Or $manager = $this->transactions; // Request a transaction $transaction = $manager->get(); // ... } } While a transaction is active, the transaction manager will always return the same transaction across the application.