[TOC]
# 模型事務
當進程執行多個數據庫操作時,可能必須成功完成每個步驟,以便維護數據完整性。事務提供了確保在將數據提交到數據庫之前已成功執行所有數據庫操作的能力。
Phalcon中的事務允許您在成功執行時提交所有操作,或者在出現問題時回滾所有操作。
## 手動事務
如果應用程序僅使用一個連接并且事務不是非常復雜,則只需將當前連接移動到事務模式,然后提交或回滾操作是否成功,就可以創建事務:
```php
<?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();
}
}
```
## 隱式事務
現有關系可用于存儲記錄及其相關實例,這種操作隱式創建事務以確保正確存儲數據:
```php
<?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();
```
## 獨立事務
隔離事務在新連接中執行,確保所有生成的SQL,虛擬外鍵檢查和業務規則與主連接隔離。這種事務需要一個事務管理器,它全局管理所創建的每個事務,確保在結束請求之前正確回滾/提交它們:
```php
<?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
<?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();
}
```
無論在何處檢索事務對象,都重用事務。僅在執行`commit()`或`rollback()`時才會生成新事務。您可以使用服務容器為整個應用程序創建全局事務管理器:
```php
<?php
use Phalcon\Mvc\Model\Transaction\Manager as TransactionManager;
$di->setShared(
'transactions',
function () {
return new TransactionManager();
}
);
```
然后從控制器或視圖訪問它:
```php
<?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();
// ...
}
}
```
當事務處于激活狀態時,事務管理器將始終在整個應用程序中返回相同的事務。
- 常規
- Welcome
- 貢獻
- 生成回溯
- 測試重現
- 單元測試
- 入門
- 安裝
- Web服務器設置
- WAMP
- XAMPP
- 教程
- 基礎教程
- 教程:創建一個簡單的REST API
- 教程:V?kuró
- 提升性能
- 教程:INVO
- 開發環境
- Phalcon Compose (Docker)
- Nanobox
- Phalcon Box (Vagrant)
- 開發工具
- Phalcon開發者工具的安裝
- Phalcon開發者工具的使用
- 調試應用程序
- 核心
- MVC應用
- 微應用
- 創建命令行(CLI)應用程序
- 依賴注入與服務定位
- MVC架構
- 服務
- 使用緩存提高性能
- 讀取配置
- 上下文轉義
- 類加載器
- 使用命名空間
- 日志
- 隊列
- 數據庫
- 數據庫抽象層
- Phalcon查詢語言(PHQL)
- ODM(對象文檔映射器)
- 使用模型
- 模型行為
- ORM緩存
- 模型事件
- 模型元數據
- 模型關系
- 模型事務
- 驗證模型
- 數據庫遷移
- 分頁
- 前端
- Assets管理
- 閃存消息
- 表單
- 圖像
- 視圖助手(標簽)
- 使用視圖
- Volt:模板引擎
- 業務邏輯
- 訪問控制列表(ACL)
- 注解解析器
- 控制器
- 調度控制器
- 事件管理器
- 過濾與清理
- 路由
- 在session中存儲數據
- 生成URL和路徑
- 驗證
- HTTP
- Cookies管理
- 請求環境
- 返回響應
- 安全
- 加密/解密
- 安全
- 國際化
- 國際化
- 多語言支持