From 846553ec13fc5b5fa5ac23b0dd45edfa9d24e64f Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 9 Dec 2016 17:52:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Query=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E7=9A=84before=5Ffind=20before=5Fselect=20after=5Finsert=20aft?= =?UTF-8?q?er=5Fupdate=20after=5Fdelete=20=E4=BA=8B=E4=BB=B6=E6=B3=A8?= =?UTF-8?q?=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 73 +++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index f0e1ae5b..bb79ac1b 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -53,6 +53,8 @@ class Query protected $bind = []; // 数据表信息 protected static $info = []; + // 回调事件 + private static $event = []; /** * 架构函数 @@ -1774,6 +1776,9 @@ class Query // 执行操作 $result = $this->execute($sql, $bind); + if ($result) { + $this->trigger('after_insert', $this); + } if ($getLastInsID) { $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); return $this->getLastInsID($sequence); @@ -1905,7 +1910,11 @@ class Query Cache::rm($key); } // 执行操作 - return '' == $sql ? 0 : $this->execute($sql, $bind); + $result = '' == $sql ? 0 : $this->execute($sql, $bind); + if ($result) { + $this->trigger('after_update', $this); + } + return $result; } } @@ -1954,12 +1963,15 @@ class Query // 获取实际执行的SQL语句 return $this->connection->getRealSql($sql, $bind); } - // 执行查询操作 - $resultSet = $this->query($sql, $bind, $options['master'], $options['fetch_class']); + if ($resultSet = $this->trigger('before_select', $this)) { + } else { + // 执行查询操作 + $resultSet = $this->query($sql, $bind, $options['master'], $options['fetch_class']); - if ($resultSet instanceof \PDOStatement) { - // 返回PDOStatement对象 - return $resultSet; + if ($resultSet instanceof \PDOStatement) { + // 返回PDOStatement对象 + return $resultSet; + } } if (isset($cache)) { @@ -2048,12 +2060,17 @@ class Query // 获取实际执行的SQL语句 return $this->connection->getRealSql($sql, $bind); } - // 执行查询 - $result = $this->query($sql, $bind, $options['master'], $options['fetch_class']); - if ($result instanceof \PDOStatement) { - // 返回PDOStatement对象 - return $result; + // 事件回调 + if ($result = $this->trigger('before_find', $this)) { + } else { + // 执行查询 + $result = $this->query($sql, $bind, $options['master'], $options['fetch_class']); + + if ($result instanceof \PDOStatement) { + // 返回PDOStatement对象 + return $result; + } } if (isset($cache)) { @@ -2251,7 +2268,11 @@ class Query Cache::rm($key); } // 执行操作 - return $this->execute($sql, $bind); + $result = $this->execute($sql, $bind); + if ($result) { + $this->trigger('after_delete', $this); + } + return $result; } /** @@ -2341,4 +2362,32 @@ class Query return $options; } + /** + * 注册回调方法 + * @access public + * @param string $event 事件名 + * @param callable $callback 回调方法 + * @return void + */ + public static function event($event, $callback) + { + self::$event[$event] = $callback; + } + + /** + * 触发事件 + * @access protected + * @param string $event 事件名 + * @param mixed $params 传入参数(引用) + * @return bool + */ + protected function trigger($event, &$params) + { + $result = false; + if (isset(self::$event[$event])) { + $callback = self::$event[$event]; + $result = call_user_func_array($callback, [ & $params]); + } + return $result; + } }