改进save方法

This commit is contained in:
thinkphp
2017-05-19 23:32:33 +08:00
parent c14988ee84
commit 53a819ff5b

View File

@@ -952,6 +952,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
}
$pk = $this->getPk();
if ($this->isUpdate) {
// 获取有更新的数据
$data = $this->getChangedData();
// 检测字段
$this->checkAllowField($data);
// 自动更新
$this->autoCompleteData($this->update);
@@ -960,9 +966,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false;
}
// 获取有更新的数据
$data = $this->getChangedData();
if (empty($data) || (count($data) == 1 && is_string($pk) && isset($data[$pk]))) {
// 关联更新
if (isset($relation)) {
@@ -978,9 +981,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$where = $this->updateWhere;
}
// 检测字段
$this->checkAllowField($data);
// 保留主键数据
foreach ($this->data as $key => $val) {
if ($this->isPk($key)) {
@@ -1008,8 +1008,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$this->trigger('after_update', $this);
} else {
// 检测字段
$this->checkAllowField($this->data);
// 自动写入
$this->autoCompleteData($this->insert);
// 自动写入创建时间和更新时间
if ($this->autoWriteTimestamp) {
if ($this->createTime && !isset($this->data[$this->createTime])) {
@@ -1024,9 +1028,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false;
}
// 检测字段
$this->checkAllowField($this->data);
$result = $this->getQuery()->insert($this->data);
// 获取自动增长主键