From e0d1f4d8b9f0f08ffb437e6eab5a9797d5594953 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 8 Apr 2016 14:12:23 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E6=95=B0=E6=8D=AE=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E6=96=B9=E6=B3=95=E6=94=B9=E4=B8=BApublic=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0insert=E5=92=8Cupdate=E5=BF=AB=E6=8D=B7=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 42 ++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index e70b8b3a..3b75d13a 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -299,20 +299,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function save($data = [], $where = []) { if (!empty($data)) { + // 数据对象赋值 foreach ($data as $key => $value) { $this->__set($key, $value); } } - // 数据自动验证 - if (!$this->dataValidate($this->data)) { + if (!$this->validateData($this->data)) { return false; } - if (false === $this->trigger('before_write', $this)) { return false; } - // 数据自动完成 $this->autoDataComplete($this->auto); @@ -321,7 +319,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (false === $this->trigger('before_update', $this)) { return false; } - // 自动更新 $this->autoDataComplete($this->update); @@ -358,17 +355,40 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $this->data[$this->pk] = $insertId; } } - // 新增回调 $this->trigger('after_insert', $this); } - // 写入回调 $this->trigger('after_write', $this); - + // 清空 + $this->isUpdate = null; return $result; } + /** + * 保存数据对象 + * @access public + * @param array $data 数据 + * @param array $where 更新条件 + * @return integer + */ + public function insert($data = []) + { + return $this->isUpdate(false)->save($data); + } + + /** + * 更新数据对象 + * @access public + * @param array $data 数据 + * @param array $where 更新条件 + * @return integer + */ + public function update($data = [], $where = []) + { + return $this->isUpdate(true)->save($data, $where); + } + /** * 是否为更新数据 * @access public @@ -456,11 +476,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 数据自动验证 - * @access protected + * @access public * @param array $data 数据 * @return void */ - protected function dataValidate(&$data) + public function validateData() { if (!empty($this->validate)) { $info = $this->validate; @@ -478,7 +498,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $validate->scene($scene); } } - if (!$validate->check($data)) { + if (!$validate->check($this->data)) { $this->error = $validate->getError(); return false; }