diff --git a/library/think/Model.php b/library/think/Model.php index 714b4277..8eaf525b 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -606,11 +606,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param array $data 数据 * @param array $where 更新条件 - * @param bool $getId 新增的时候是否获取id - * @param bool $replace 是否replace + * @param string $sequence 自增序列名 * @return integer */ - public function save($data = [], $where = [], $getId = true, $replace = false) + public function save($data = [], $where = [], $sequence = null) { if (!empty($data)) { // 数据自动验证 @@ -703,16 +702,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return false; } - $result = $this->db()->insert($this->data, $replace); + $result = $this->db()->insert($this->data); // 获取自动增长主键 - if ($result && $getId) { - $insertId = $this->db()->getLastInsID(); + if ($result) { + $insertId = $this->db()->getLastInsID($sequence); $pk = $this->getPk(); if (is_string($pk) && $insertId) { $this->data[$pk] = $insertId; } - $result = $insertId; } // 标记为更新 $this->isUpdate = true; @@ -954,14 +952,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * 写入数据 * @access public * @param array $data 数据数组 - * @param bool $replace 是否replace - * @param bool $getId 是否返回自增主键 * @return $this */ - public static function create($data = [], $replace = false, $getId = true) + public static function create($data = []) { $model = new static(); - $model->isUpdate(false)->save($data, [], $getId, $replace); + $model->isUpdate(false)->save($data, []); return $model; } diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 40263e49..bbdbdc6a 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -365,13 +365,11 @@ abstract class Connection * @access public * @param string $sql sql指令 * @param array $bind 参数绑定 - * @param boolean $getLastInsID 是否获取自增ID - * @param string $sequence 自增序列名 * @return int * @throws BindParamException * @throws PDOException */ - public function execute($sql, $bind = [], $getLastInsID = false, $sequence = null) + public function execute($sql, $bind = []) { $this->initConnect(true); if (!$this->linkID) { @@ -399,12 +397,6 @@ abstract class Connection $this->debug(false); $this->numRows = $this->PDOStatement->rowCount(); - if (preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $sql)) { - $this->lastInsID = $this->linkID->lastInsertId($sequence); - if ($getLastInsID) { - return $this->lastInsID; - } - } return $this->numRows; } catch (\PDOException $e) { throw new PDOException($e, $this->config, $this->queryStr); @@ -702,10 +694,14 @@ abstract class Connection /** * 获取最近插入的ID * @access public + * @param string $sequence 自增序列名 * @return string */ - public function getLastInsID() + public function getLastInsID($sequence = null) { + if (is_null($this->lastInsID)) { + $this->lastInsID = $this->linkID->lastInsertId($sequence); + } return $this->lastInsID; } diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 8dcfcfa0..3a687f13 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -194,25 +194,24 @@ class Query * @access public * @param string $sql sql指令 * @param array $bind 参数绑定 - * @param boolean $getLastInsID 是否获取自增ID - * @param boolean $sequence 自增序列名 * @return int * @throws BindParamException * @throws PDOException */ - public function execute($sql, $bind = [], $getLastInsID = false, $sequence = null) + public function execute($sql, $bind = []) { - return $this->connection->execute($sql, $bind, $getLastInsID, $sequence); + return $this->connection->execute($sql, $bind); } /** * 获取最近插入的ID * @access public + * @param string $sequence 自增序列名 * @return string */ - public function getLastInsID() + public function getLastInsID($sequence = null) { - return $this->connection->getLastInsID(); + return $this->connection->getLastInsID($sequence); } /** @@ -1667,7 +1666,7 @@ class Query * @access public * @param mixed $data 数据 * @param boolean $replace 是否replace - * @param boolean $getLastInsID 是否获取自增ID + * @param boolean $getLastInsID 返回自增主键 * @param string $sequence 自增序列名 * @return integer|string */ @@ -1683,9 +1682,14 @@ class Query // 获取实际执行的SQL语句 return $this->connection->getRealSql($sql, $bind); } - $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); + // 执行操作 - return $this->execute($sql, $bind, $getLastInsID, $sequence); + $result = $this->execute($sql, $bind); + if ($getLastInsID) { + $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); + return $this->getLastInsID($sequence); + } + return $result; } /**