diff --git a/library/think/Model.php b/library/think/Model.php index a96c7d6e..561792aa 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -649,7 +649,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (false === $this->trigger('before_write', $this)) { return false; } - + $pk = $this->getPk(); if ($this->isUpdate) { // 自动更新 $this->autoCompleteData($this->update); @@ -680,7 +680,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $where = $this->updateWhere; } - $pk = $this->getPk(); if (is_string($pk) && isset($data[$pk])) { if (!isset($where[$pk])) { unset($where); @@ -710,10 +709,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $result = $this->db()->insert($this->data); // 获取自动增长主键 - if ($result) { + if ($result && is_string($pk) && !isset($this->data[$pk])) { $insertId = $this->db()->getLastInsID($sequence); - $pk = $this->getPk(); - if (is_string($pk) && $insertId) { + if ($insertId) { $this->data[$pk] = $insertId; } }