From ff85b8c3cfdafa4428e4934c357c4479e1e41eb9 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 30 Jan 2017 14:29:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bafter=5Finsert=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E6=96=B9=E6=B3=95=E7=9A=84=E5=8F=82=E6=95=B0=20?= =?UTF-8?q?=E4=BC=A0=E5=85=A5=E6=9C=80=E7=BB=88=E7=9A=84data=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=20=E5=8C=85=E6=8B=AC=E8=87=AA=E5=A2=9EID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index a9d1a543..3736f85e 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2070,11 +2070,17 @@ class Query // 执行操作 $result = $this->execute($sql, $bind); if ($result) { - $this->trigger('after_insert', $options); - } - if ($getLastInsID) { - $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); - return $this->getLastInsID($sequence); + $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); + $lastInsId = $this->getLastInsID($sequence); + if ($lastInsId) { + $pk = $this->getPk($options); + $data[$pk] = $lastInsId; + } + $this->trigger('after_insert', $data); + + if ($getLastInsID) { + return $lastInsId; + } } return $result; } @@ -2190,6 +2196,7 @@ class Query } elseif (!isset($key) && is_string($pk) && isset($options['where']['AND'][$pk])) { $key = $this->getCacheKey($options['where']['AND'][$pk], $options); } + // 生成UPDATE SQL语句 $sql = $this->builder->update($data, $options); // 获取参数绑定 @@ -2728,15 +2735,15 @@ class Query * 触发事件 * @access protected * @param string $event 事件名 - * @param mixed $options 当前查询参数 + * @param mixed $params 额外参数 * @return bool */ - protected function trigger($event, $options = []) + protected function trigger($event, $params = []) { $result = false; if (isset(self::$event[$event])) { $callback = self::$event[$event]; - $result = call_user_func_array($callback, [$options, $this]); + $result = call_user_func_array($callback, [$params, $this]); } return $result; }