model改进

This commit is contained in:
thinkphp
2016-04-09 13:13:15 +08:00
parent 7fb2a9e291
commit 5a70087133

View File

@@ -337,24 +337,26 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
if (!$this->validateData()) { if (!$this->validateData()) {
return false; return false;
} }
if (false === $this->trigger('before_write', $this)) {
return false;
}
// 数据自动完成 // 数据自动完成
$this->autoCompleteData($this->auto); $this->autoCompleteData($this->auto);
// 事件回调
if (false === $this->trigger('before_write', $this)) {
return false;
}
if ($this->isUpdate) { if ($this->isUpdate) {
// 更新数据
// 自动更新 // 自动更新
$this->autoCompleteData($this->update); $this->autoCompleteData($this->update);
// 事件回调
if (false === $this->trigger('before_update', $this)) { if (false === $this->trigger('before_update', $this)) {
return false; return false;
} }
// 去除没有更新的字段 // 去除没有更新的字段
foreach ($this->data as $key => $val) { foreach ($this->data as $key => $val) {
if (!in_array($key, $this->change) && !$this->isPk($key) && !isset($this->relation[$key])) { if (!in_array($key, $this->change) && !$this->isPk($key)) {
unset($this->data[$key]); unset($this->data[$key]);
} }
} }
@@ -368,7 +370,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 更新回调 // 更新回调
$this->trigger('after_update', $this); $this->trigger('after_update', $this);
} else { } else {
// 新增数据
// 自动写入 // 自动写入
$this->autoCompleteData($this->insert); $this->autoCompleteData($this->insert);