From ebde8513dc0052860a11cf7fe88937e9752fa237 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 10 Feb 2016 19:18:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=9A=84=E5=86=99=E5=85=A5?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=B9=9F=E5=8F=AF=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=20=E8=87=AA=E5=8A=A8=E9=AA=8C=E8=AF=81=E5=92=8C=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 63 ++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 38373d67..d426f236 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -237,6 +237,16 @@ class Model } } } + + // 数据自动验证 + $this->dataValidate($data); + if (!empty($this->error)) { + return false; + } + + // 数据自动填充 + $this->dataFill($data); + $fields = $this->getFields(); // 检查非数据字段 if (!empty($fields)) { @@ -281,6 +291,9 @@ class Model { // 数据处理 $data = $this->_write_data($data, 'insert'); + if(false === $data){ + return false; + } // 分析表达式 $options = $this->_parseOptions(); if (false === $this->_before_insert($data, $options)) { @@ -319,7 +332,11 @@ class Model } // 数据处理 foreach ($dataList as $key => $data) { - $dataList[$key] = $this->_write_data($data, 'insert'); + $data = $this->_write_data($data, 'insert'); + if(false === $data){ + return false; + } + $dataList[$key] = $data; } // 分析表达式 $options = $this->_parseOptions($options); @@ -342,6 +359,9 @@ class Model { // 数据处理 $data = $this->_write_data($data, 'update'); + if(false === $data){ + return false; + } // 分析表达式 $options = $this->_parseOptions(); $pk = $this->getPk($options['table']); @@ -932,6 +952,26 @@ class Model } // 数据自动验证 + $this->dataValidate($data); + if (!empty($this->error)) { + return false; + } + + // 数据自动填充 + $this->dataFill($data); + + // 过滤创建的数据 + $this->_create_filter($data); + // 赋值当前数据对象 + $this->data = $data; + // 返回创建的数据以供其他调用 + return $data; + } + // 数据对象创建后的回调方法 + protected function _create_filter(&$data) + {} + + protected function dataValidate(&$data){ if (!empty($this->options['validate'])) { if (is_string($this->options['validate'])) { // 读取配置文件中的自动验证定义 @@ -981,17 +1021,16 @@ class Model $this->error[$key] = $result; } else { $this->error = $result; - return false; + return; } } } - // 批量验证的时候最后返回错误 - if (!empty($this->error)) { - return false; - } + $this->options['validate'] = null; } + return ; + } - // 数据自动填充 + protected function dataFill(&$data){ if (!empty($this->options['auto'])) { if (is_string($this->options['auto'])) { // 读取配置文件中的自动验证定义 @@ -1007,17 +1046,9 @@ class Model // 数据自动填充 $this->autoOperation($key, $val, $data); } + $this->options['auto'] = null; } - // 过滤创建的数据 - $this->_create_filter($data); - // 赋值当前数据对象 - $this->data = $data; - // 返回创建的数据以供其他调用 - return $data; } - // 数据对象创建后的回调方法 - protected function _create_filter(&$data) - {} /** * 数据自动填充