diff --git a/library/think/Model.php b/library/think/Model.php index 39390a30..e70b8b3a 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -61,6 +61,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $type = []; // 当前执行的关联类型 private $relation; + // 是否为更新数据 + protected $isUpdate; /** * 架构函数 @@ -288,12 +290,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } /** - * 保存当前数据对象的值 + * 保存当前数据对象 * @access public * @param array $data 数据 + * @param array $where 更新条件 * @return integer */ - public function save($data = []) + public function save($data = [], $where = []) { if (!empty($data)) { foreach ($data as $key => $value) { @@ -311,36 +314,54 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } // 数据自动完成 - foreach ($this->auto as $field) { - if (!in_array($field, $this->change)) { - // 没有经过修改器赋值则进行自动完成 - $this->__set($field, isset($this->data[$field]) ? $this->data[$field] : null); + $this->autoDataComplete($this->auto); + + if ($this->isUpdate()) { + // 更新数据 + if (false === $this->trigger('before_update', $this)) { + return false; } - } - if (false === $this->trigger('before_insert', $this)) { - return false; - } + // 自动更新 + $this->autoDataComplete($this->update); - // 自动写入 - foreach ($this->insert as $field) { - if (!in_array($field, $this->change)) { - $this->__set($field, isset($this->data[$field]) ? $this->data[$field] : null); + // 去除没有更新的字段 + foreach ($this->data as $key => $val) { + if (!in_array($key, $this->change) && !$this->isPk($key) && !isset($this->relation[$key])) { + unset($this->data[$key]); + } } - } - $result = self::db()->insert($this->data); - - // 获取自动增长主键 - if ($result) { - $insertId = self::db()->getLastInsID(); - if (is_string($this->pk) && $insertId) { - $this->data[$this->pk] = $insertId; + $db = self::db(); + if (!empty($where)) { + $db->where($where); } - } + $result = $db->update($this->data); - // 新增回调 - $this->trigger('after_insert', $this); + // 更新回调 + $this->trigger('after_update', $this); + } else { + // 新增数据 + if (false === $this->trigger('before_insert', $this)) { + return false; + } + + // 自动写入 + $this->autoDataComplete($this->insert); + + $result = self::db()->insert($this->data); + + // 获取自动增长主键 + if ($result) { + $insertId = self::db()->getLastInsID(); + if (is_string($this->pk) && $insertId) { + $this->data[$this->pk] = $insertId; + } + } + + // 新增回调 + $this->trigger('after_insert', $this); + } // 写入回调 $this->trigger('after_write', $this); @@ -349,68 +370,51 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } /** - * 更新当前数据对象 + * 是否为更新数据 * @access public - * @param array $data 数据 - * @param array $where 更新条件 - * @return integer + * @param bool|null $update + * @return void */ - public function update($data = [], $where = []) + protected function isUpdate($update = null) { - if (!empty($data)) { - foreach ($data as $key => $value) { - $this->__set($key, $value); + if (is_bool($update)) { + $this->isUpdate = $update; + return $this; + } + + if (isset($this->isUpdate)) { + return $this->isUpdate; + } + + // 根据主键判断是否更新 + $data = $this->data; + $pk = $this->pk; + if (is_string($pk) && isset($data[$pk])) { + return true; + } elseif (is_array($pk)) { + foreach ($pk as $field) { + if (isset($data[$field])) { + return true; + } } } + // TODO 完善没有主键或者其他的情况 + return false; + } - // 数据自动验证 - if (!$this->dataValidate($this->data)) { - return false; - } - - if (false === $this->trigger('before_write', $this)) { - return false; - } - - // 数据自动完成 - foreach ($this->auto as $field) { - if (!in_array($field, $this->change)) { - // 没有经过修改器赋值则进行自动完成 - $this->__set($field, isset($this->data[$field]) ? $this->data[$field] : null); - } - } - - if (false === $this->trigger('before_update', $this)) { - return false; - } - - // 自动更新 - foreach ($this->update as $field) { + /** + * 数据自动完成 + * @access public + * @param array $auto 要自动更新的字段列表 + * @return void + */ + protected function autoDataComplete($auto = []) + { + foreach ($auto as $field) { if (!in_array($field, $this->change)) { $this->__set($field, isset($this->data[$field]) ? $this->data[$field] : null); } } - - // 去除没有更新的字段 - foreach ($this->data as $key => $val) { - if (!in_array($key, $this->change) && !$this->isPk($key) && !isset($this->relation[$key])) { - unset($this->data[$key]); - } - } - - $db = self::db(); - if (!empty($where)) { - $db->where($where); - } - $result = $db->update($this->data); - - // 更新回调 - $this->trigger('after_update', $this); - - // 写入回调 - $this->trigger('after_write', $this); - - return $result; } /**