From 57e72fcee02af7e244fb829b3c0ce5d9a473e9ce Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 3 Jun 2016 16:07:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E9=AA=8C=E8=AF=81=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index de63a594..de3c9142 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -299,6 +299,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function save($data = [], $where = [], $getId = true) { if (!empty($data)) { + // 数据自动验证 + if (!$this->validateData($data)) { + return false; + } // 数据对象赋值 foreach ($data as $key => $value) { $this->__set($key, $value); @@ -307,10 +311,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $this->isUpdate = true; } } - // 数据自动验证 - if (!$this->validateData()) { - return false; - } // 检测字段 if (!empty($this->field)) { @@ -517,9 +517,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 自动验证当前数据对象值 * @access public + * @param array $data 验证数据 * @return bool */ - public function validateData() + public function validateData($data) { if (!empty($this->validate)) { $info = $this->validate; @@ -537,7 +538,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $validate->scene($scene); } } - if (!$validate->check($this->data)) { + if (!$validate->check($data)) { $this->error = $validate->getError(); return false; }