# 事務管理(Model Transactions)
當一個進程執行多個數據庫操作時,通常需要每一步都是成功完成以便保證數據完整性。事務可以確保在數據提交到數據庫保存之前所有數據庫操作都成功執行。
Phalcon中通過事務,可以在所有操作都成功執行之后提交到服務器,或者當有錯誤發生時回滾所有的操作。
## 自定義事務(Manual Transactions)
如果一個應用只用到了一個數據庫連接并且這些事務都不太復雜,那么可以通過簡單的將當前數據庫連接設置成事務模式實現事務功能,根據操作的成功與否提交或者回滾:
~~~
<?php
use Phalcon\Mvc\Controller;
class RobotsController extends Controller
{
public function saveAction()
{
// Start a transaction
$this->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();
}
}
~~~
## 隱含的事務(Implicit Transactions)
也可以通過已有的關系來存儲記錄以及其相關記錄,這種操作將隱式地創建一個事務來保證所有數據都能夠正確地保存:
~~~
<?php
$robotPart = new RobotParts();
$robotPart->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)
單獨事務在一個新的連接中執行所有的SQL,虛擬外鍵檢查和業務規則與主數據庫連接是相互獨立的。 這種事務需要一個事務管理器來全局的管理每一個事務,保證他們在請求結束前能正確的回滾或者提交。
~~~
<?php
use Phalcon\Mvc\Model\Transaction\Failed as TxFailed;
use Phalcon\Mvc\Model\Transaction\Manager 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's gone fine, let's commit the transaction
$transaction->commit();
} catch (TxFailed $e) {
echo "Failed, reason: ", $e->getMessage();
}
~~~
事務可以用以保證以一致性的方式刪除多條記錄:
~~~
<?php
use Phalcon\Mvc\Model\Transaction\Failed as TxFailed;
use Phalcon\Mvc\Model\Transaction\Manager as TxManager;
try {
// Create a transaction manager
$manager = new TxManager();
// Request a transaction
$transaction = $manager->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();
}
~~~
事務對象可以重用,不管事務對象是在什么地方獲取的。只有當一個:code:[`](http://docs.iphalcon.cn/reference/model-transactions.html#id1)commit()`或者一個:code:[`](http://docs.iphalcon.cn/reference/model-transactions.html#id3)rollback()`執行時才會創建一個新的事務對象。可以通過服務容器在整個應用中來創建和管理全局事務管理器。
~~~
<?php
use Phalcon\Mvc\Model\Transaction\Manager as TransactionManager
$di->setShared(
"transactions",
function () {
return new TransactionManager();
}
);
~~~
然后在控制器或者視圖中訪問:
~~~
<?php
use Phalcon\Mvc\Controller;
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();
// ...
}
}
~~~
當一個事務處于活動狀態時,在整個應用中事務管理器將總是返回這個相同的事務。
- 簡介
- 安裝
- 安裝(installlation)
- XAMPP下的安裝
- WAMP下安裝
- Nginx安裝說明
- Apache安裝說明
- Cherokee 安裝說明
- 使用 PHP 內置 web 服務器
- Phalcon 開發工具
- Linux 系統下使用 Phalcon 開發工具
- Mac OS X 系統下使用 Phalcon 開發工具
- Windows 系統下使用 Phalcon 開發工具
- 教程
- 教程 1:讓我們通過例子來學習
- 教程 2:INVO簡介
- 教程 3: 保護INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程 6: V?kuró
- 教程 7:創建簡單的 REST API
- 組件
- 依賴注入與服務定位器
- MVC架構
- 使用控制器
- 使用模型
- 模型關系
- 事件與事件管理器
- Behaviors
- 模型元數據
- 事務管理
- 驗證數據完整性
- Workingwith Models
- Phalcon查詢語言
- 緩存對象關系映射
- 對象文檔映射 ODM
- 使用視圖
- 視圖助手
- 資源文件管理
- Volt 模版引擎
- MVC 應用
- 路由
- 調度控制器
- Micro Applications
- 使用命名空間
- 事件管理器
- Request Environmen
- 返回響應
- Cookie 管理
- 生成 URL 和 路徑
- 閃存消息
- 使用 Session 存儲數據
- 過濾與清理
- 上下文編碼
- 驗證Validation
- 表單_Forms
- 讀取配置
- 分頁 Pagination
- 使用緩存提高性能
- 安全
- 加密與解密 Encryption/Decryption
- 訪問控制列表
- 多語言支持
- 類加載器 Class Autoloader
- 日志記錄_Logging
- 注釋解析器 Annotations Parser
- 命令行應用 Command Line Applications
- Images
- 隊列 Queueing
- 數據庫抽象層
- 國際化
- 數據庫遷移
- 調試應用程序
- 單元測試
- 進階技巧與延伸閱讀
- 提高性能:下一步該做什么?
- Dependency Injection Explained
- Understanding How Phalcon Applications Work
- Api
- Abstract class Phalcon\Acl