From 42073f04f98bc566ae3d777dbcaab90a3500f384 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 10 Apr 2016 14:30:26 +0800 Subject: [PATCH] =?UTF-8?q?Db=E7=B1=BB=E5=A2=9E=E5=8A=A0=20transaction=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=94=A8=E4=BA=8E=E6=89=A7=E8=A1=8C=E6=9F=90?= =?UTF-8?q?=E7=BB=84=E6=95=B0=E6=8D=AE=E4=BA=8B=E5=8A=A1=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Driver.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/library/think/db/Driver.php b/library/think/db/Driver.php index 2e079072..1045c53f 100644 --- a/library/think/db/Driver.php +++ b/library/think/db/Driver.php @@ -366,6 +366,24 @@ abstract class Driver return $result; } + /** + * 执行数据库事务 + * @access public + * @param callable $callback 数据操作方法回调 + * @return void + */ + public function transaction($callback){ + $this->startTrans(); + try { + if (is_callable($callback)) { + call_user_func_array($callback, [$sql, $runtime, $explain]); + } + $this->commit(); + }catch (\PDOException $e) { + $this->rollback() + } + } + /** * 启动事务 * @access public