<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [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(); // ... } } ``` 當事務處于激活狀態時,事務管理器將始終在整個應用程序中返回相同的事務。
                  <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>

                              哎呀哎呀视频在线观看