diff --git a/library/think/Model.php b/library/think/Model.php index d387296e..e7b5dcf3 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -207,7 +207,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param array $where 更新条件 * @return integer */ - public function save($data = [], $where = [], $getInsertId = true) + public function save($data = [], $where = []) { if (!empty($data)) { // 数据对象赋值 @@ -270,12 +270,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $result = self::db()->insert($this->data); // 获取自动增长主键 - if ($result && $getInsertId) { + if ($result) { $insertId = self::db()->getLastInsID(); $pk = $this->getPk(); if (is_string($pk) && $insertId) { $this->data[$pk] = $insertId; } + $result = $insertId; } // 新增回调 $this->trigger('after_insert', $this); diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 59b2c0ca..69cd7a9d 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -303,9 +303,10 @@ abstract class Connection * @param string $sql sql指令 * @param array $bind 参数绑定 * @param boolean $fetch 不执行只是获取SQL + * @param boolean $getLastInsID 是否获取自增ID * @return integer */ - public function execute($sql, $bind = [], $fetch = false) + public function execute($sql, $bind = [], $fetch = false, $getLastInsID = false) { $this->initConnect(true); if (!$this->linkID) { @@ -338,6 +339,9 @@ abstract class Connection $this->numRows = $this->PDOStatement->rowCount(); if (preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $sql)) { $this->lastInsID = $this->linkID->lastInsertId(); + if ($getLastInsID) { + return $this->lastInsID; + } } return $this->numRows; } catch (\PDOException $e) { diff --git a/library/think/db/Query.php b/library/think/db/Query.php index ced89e49..f3089ee3 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1094,16 +1094,29 @@ class Query * @access public * @param mixed $data 数据 * @param boolean $replace 是否replace + * @param boolean $getLastInsID 是否获取自增ID * @return integer */ - public function insert(array $data, $replace = false) + public function insert(array $data, $replace = false, $getLastInsID = false) { // 分析查询表达式 $options = $this->parseExpress(); // 生成SQL语句 $sql = $this->builder()->insert($data, $options, $replace); // 执行操作 - return $this->connection->execute($sql, $this->getBind(), $options['fetch_sql']); + return $this->connection->execute($sql, $this->getBind(), $options['fetch_sql'], $getLastInsID); + } + + /** + * 插入记录并获取自增ID + * @access public + * @param mixed $data 数据 + * @param boolean $replace 是否replace + * @return integer + */ + public function insertGetId(array $data, $replace = false) + { + return $this->insert($data, $replace, true); } /** diff --git a/library/think/model/Merge.php b/library/think/model/Merge.php index 541334d4..9ef94a70 100644 --- a/library/think/model/Merge.php +++ b/library/think/model/Merge.php @@ -145,7 +145,7 @@ class Merge extends Model * @param array $where 更新条件 * @return mixed */ - public function save($data = [], $where = [], $getInsertId = true) + public function save($data = [], $where = []) { if (!empty($data)) { // 数据对象赋值 @@ -211,6 +211,7 @@ class Merge extends Model $data = $this->parseData($name, $this->data, true); self::db()->table($table)->strict(false)->insert($data); } + $result = $insertId; } // 新增回调 $this->trigger('after_insert', $this);