改进Model类的save方法 新增数据的时候添加不是自增主键的处理

This commit is contained in:
thinkphp
2016-10-19 17:23:34 +08:00
parent 4ceee7c5b2
commit d0cc9a7146

View File

@@ -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;
}
}