改进after_insert回调方法的参数 传入最终的data数据 包括自增ID

This commit is contained in:
thinkphp
2017-01-30 14:29:12 +08:00
parent f12adbb943
commit ff85b8c3cf

View File

@@ -2070,11 +2070,17 @@ class Query
// 执行操作 // 执行操作
$result = $this->execute($sql, $bind); $result = $this->execute($sql, $bind);
if ($result) { if ($result) {
$this->trigger('after_insert', $options); $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
} $lastInsId = $this->getLastInsID($sequence);
if ($getLastInsID) { if ($lastInsId) {
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); $pk = $this->getPk($options);
return $this->getLastInsID($sequence); $data[$pk] = $lastInsId;
}
$this->trigger('after_insert', $data);
if ($getLastInsID) {
return $lastInsId;
}
} }
return $result; return $result;
} }
@@ -2190,6 +2196,7 @@ class Query
} elseif (!isset($key) && is_string($pk) && isset($options['where']['AND'][$pk])) { } elseif (!isset($key) && is_string($pk) && isset($options['where']['AND'][$pk])) {
$key = $this->getCacheKey($options['where']['AND'][$pk], $options); $key = $this->getCacheKey($options['where']['AND'][$pk], $options);
} }
// 生成UPDATE SQL语句 // 生成UPDATE SQL语句
$sql = $this->builder->update($data, $options); $sql = $this->builder->update($data, $options);
// 获取参数绑定 // 获取参数绑定
@@ -2728,15 +2735,15 @@ class Query
* 触发事件 * 触发事件
* @access protected * @access protected
* @param string $event 事件名 * @param string $event 事件名
* @param mixed $options 当前查询参数 * @param mixed $params 额外参数
* @return bool * @return bool
*/ */
protected function trigger($event, $options = []) protected function trigger($event, $params = [])
{ {
$result = false; $result = false;
if (isset(self::$event[$event])) { if (isset(self::$event[$event])) {
$callback = 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; return $result;
} }