<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 事務管理(Model Transactions) # 事務管理(Model Transactions) 當一個進程執行多個數據庫操作時,通常需要每一步都是成功完成以便保證數據完整性。事務可以確保在數據提交到數據庫保存之前所有數據庫操作都成功執行。 Phalcon中通過事務,可以在所有操作都成功執行之后提交到服務器,或者當有錯誤發生時回滾所有的操作。 ### 自定義事務(Manual Transactions) 如果一個應用只用到了一個數據庫連接并且這些事務都不太復雜,那么可以通過簡單的將當前數據庫連接設置成事務模式實現事務功能,根據操作的成功與否提交或者回滾: ``` <pre class="calibre14">``` <?php use Phalcon\Mvc\Controller; 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(); } } ``` ``` ### 隱含的事務(Implicit Transactions) 也可以通過已有的關系來存儲記錄以及其相關記錄,這種操作將隱式的創建一個事務來保證所有數據能夠正確的保存: ``` <pre class="calibre14">``` <?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 ``` ``` ### 單獨的事務(Isolated Transactions) 單獨事務在一個新的連接中執行所有的SQL,虛擬外鍵檢查和業務規則與主數據庫連接是相互獨立的。這種事務需要一個事務管理器來全局的管理每一個事務,保證他們在請求結束前能正確的回滾或者提交。 ``` <pre class="calibre14">``` <?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 goes fine, let's commit the transaction $transaction->commit(); } catch (TxFailed $e) { echo "Failed, reason: ", $e->getMessage(); } ``` ``` 事務可以用以保證以一致性的方式刪除多條記錄: ``` <pre class="calibre14">``` <?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 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()執行時才會創建一個新的事務對象。可以通過服務容器在整個應用中來創建和管理全局師傅管理器。 ``` <pre class="calibre14">``` <?php use Phalcon\Mvc\Model\Transaction\Manager as TransactionManager $di->setShared('transactions', function () { return new TransactionManager(); }); ``` ``` 然后在控制器或者視圖中訪問: ``` <pre class="calibre14">``` <?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(); // ... } } ``` ``` While a transaction is active, the transaction manager will always return the same transaction across the application. | - [索引](# "總目錄") - [下一頁](# "Phalcon 查詢語言(Phalcon Query Language (PHQL))") | - [上一頁](# "模型元數據(Models Meta-Data)") |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看