发布于 2015-08-21 15:13:30 | 1443 次阅读 | 评论: 0 | 来源: 网络整理
当一个进程执行多个数据库操作时,通常需要每一步都是成功完成以便保证数据完整性。事务可以确保在数据提交到数据库保存之前所有数据库操作都成功执行。
Phalcon中通过事务,可以在所有操作都成功执行之后提交到服务器,或者当有错误发生时回滚所有的操作。
如果一个应用只用到了一个数据库连接并且这些事务都不太复杂,那么可以通过简单的将当前数据库连接设置成事务模式实现事务功能,根据操作的成功与否提交或者回滚:
<?php
use PhalconMvcController;
class RobotsController extends Controller
{
public function saveAction()
{
$this->db->begin();
$robot = new Robots();
$robot->name = "WALL·E";
$robot->created_at = date("Y-m-d");
if ($robot->save() == false) {
$this->db->rollback();
return;
}
$robotPart = new RobotParts();
$robotPart->robots_id = $robot->id;
$robotPart->type = "head";
if ($robotPart->save() == false) {
$this->db->rollback();
return;
}
$this->db->commit();
}
}
也可以通过已有的关系来存储记录以及其相关记录,这种操作将隐式的创建一个事务来保证所有数据能够正确的保存:
<?php
$robotPart = new RobotParts();
$robotPart->type = "head";
$robot = new Robots();
$robot->name = "WALL·E";
$robot->created_at = date("Y-m-d");
$robot->robotPart = $robotPart;
$robot->save(); // Creates an implicit transaction to store both records
单独事务在一个新的连接中执行所有的SQL,虚拟外键检查和业务规则与主数据库连接是相互独立的。 这种事务需要一个事务管理器来全局的管理每一个事务,保证他们在请求结束前能正确的回滚或者提交。
<?php
use PhalconMvcModelTransactionFailed as TxFailed;
use PhalconMvcModelTransactionManager as TxManager;
try {
// Create a transaction manager
$manager = new TxManager();
// Request a transaction
$transaction = $manager->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 goes fine, let's commit the transaction
$transaction->commit();
} catch (TxFailed $e) {
echo "Failed, reason: ", $e->getMessage();
}
事务可以用以保证以一致性的方式删除多条记录:
<?php
use PhalconMvcModelTransactionFailed as TxFailed;
use PhalconMvcModelTransactionManager as TxManager;
try {
// Create a transaction manager
$manager = new TxManager();
// Request a transaction
$transaction = $manager->get();
// Get the robots will be deleted
foreach (Robots::find("type = 'mechanical'") as $robot) {
$robot->setTransaction($transaction);
if ($robot->delete() == false) {
// Something goes wrong, we should to rollback the transaction
foreach ($robot->getMessages() as $message) {
$transaction->rollback($message->getMessage());
}
}
}
// Everything goes fine, let's commit the transaction
$transaction->commit();
echo "Robots were deleted successfully!";
} catch (TxFailed $e) {
echo "Failed, reason: ", $e->getMessage();
}
事务对象可以重用,不管事务对象是在什么地方获取的。只有当一个commit()或者一个rollback()执行时才会创建一个新的事务对象。可以通过服务容器在整个应用中来创建和管理全局师傅管理器。
<?php
use PhalconMvcModelTransactionManager as TransactionManager
$di->setShared('transactions', function () {
return new TransactionManager();
});
然后在控制器或者视图中访问:
<?php
use PhalconMvcController;
class ProductsController extends Controller
{
public function saveAction()
{
// Obtain the TransactionsManager from the services container
$manager = $this->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.