From b6b8b6dcb42600391d40658844ce4edf53c54846 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 13 Jun 2016 11:09:42 +0800 Subject: [PATCH] =?UTF-8?q?Controller=E7=B1=BB=E5=A2=9E=E5=8A=A0batchValid?= =?UTF-8?q?ate=E5=B1=9E=E6=80=A7=20validate=E6=96=B9=E6=B3=95=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0batch=E5=8F=82=E6=95=B0=EF=BC=8C=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=98=AF=E5=90=A6=E6=89=B9=E9=87=8F=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=20=E5=BC=82=E5=B8=B8=E7=B1=BB=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Controller.php | 11 +++++++++-- library/think/Loader.php | 8 ++++---- library/think/Session.php | 2 +- library/think/Template.php | 2 +- library/think/db/Query.php | 6 +++++- .../db/exception/DataNotFoundException.php | 10 +++++++++- .../db/exception/ModelNotFoundException.php | 5 +++++ .../think/exception/ClassNotFoundException.php | 16 ++++++++++++++++ .../exception/TemplateNotFoundException.php | 17 +++++++++++++++++ library/think/exception/ValidateException.php | 16 ++++++++++++++++ library/think/view/driver/Php.php | 2 +- library/think/view/driver/Think.php | 2 +- 12 files changed, 85 insertions(+), 12 deletions(-) diff --git a/library/think/Controller.php b/library/think/Controller.php index dbb4aeae..74e69ef2 100644 --- a/library/think/Controller.php +++ b/library/think/Controller.php @@ -28,6 +28,8 @@ class Controller protected $request; // 验证失败是否抛出异常 protected $failException = false; + // 是否批量验证 + protected $batchValidate = false; /** * 前置操作方法列表 @@ -162,10 +164,11 @@ class Controller * @param array $data 数据 * @param string|array $validate 验证器名或者验证规则数组 * @param array $message 提示信息 + * @param bool $batch 是否批量验证 * @param mixed $callback 回调方法(闭包) * @return true|string|array */ - protected function validate($data, $validate, $message = [], $callback = null) + protected function validate($data, $validate, $message = [], $batch = false, $callback = null) { if (is_array($validate)) { $v = Loader::validate(); @@ -180,12 +183,16 @@ class Controller $v->scene($scene); } } + // 是否批量验证 + if($batch || $this->batchValidate){ + $v->batch(true); + } if (is_array($message)) { $v->message($message); } - if (is_callable($callback)) { + if ($callback && is_callable($callback)) { call_user_func_array($callback, [$v, &$data]); } diff --git a/library/think/Loader.php b/library/think/Loader.php index 7972ade4..27ad52f2 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -291,7 +291,7 @@ class Loader if (class_exists($class)) { $model = new $class(); } else { - throw new ClassNotFoundException('class [ ' . $class . ' ] not exists'); + throw new ClassNotFoundException('class [ ' . $class . ' ] not exists', $class); } } $_model[$name . $layer] = $model; @@ -327,7 +327,7 @@ class Loader } elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) { return new $emptyClass(Request::instance()); } else { - throw new ClassNotFoundException('class [ ' . $class . ' ] not exists'); + throw new ClassNotFoundException('class [ ' . $class . ' ] not exists', $class); } } @@ -364,7 +364,7 @@ class Loader if (class_exists($class)) { $validate = new $class; } else { - throw new ClassNotFoundException('class [ ' . $class . ' ] not exists'); + throw new ClassNotFoundException('class [ ' . $class . ' ] not exists', $class); } } $_instance[$name . $layer] = $validate; @@ -428,7 +428,7 @@ class Loader $_instance[$identify] = $o; } } else { - throw new ClassNotFoundException('class not exist :' . $class); + throw new ClassNotFoundException('class not exist :' . $class, $class); } } return $_instance[$identify]; diff --git a/library/think/Session.php b/library/think/Session.php index 193165c9..e9855a9e 100644 --- a/library/think/Session.php +++ b/library/think/Session.php @@ -94,7 +94,7 @@ class Session // 检查驱动类 if (!class_exists($class) || !session_set_save_handler(new $class($config))) { - throw new ClassNotFoundException('error session handler'); + throw new ClassNotFoundException('error session handler', $class); } } if ($isDoStart) { diff --git a/library/think/Template.php b/library/think/Template.php index ab14b12c..b1d6943b 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -1068,7 +1068,7 @@ class Template $this->includeFile[$template] = filemtime($template); return $template; } else { - throw new TemplateNotFoundException('template not exist:' . $template); + throw new TemplateNotFoundException('template not exist:' . $template, $template); } } diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 4eee12ca..c82e5db8 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1723,7 +1723,11 @@ class Query } } } elseif (!empty($options['fail'])) { - throw new DbException('Data not Found', $options, $sql); + if(!empty($this->model)){ + throw new ModelNotFoundException('Data not Found', $this->model, $options); + }else{ + throw new DataNotFoundException('Data not Found', $options['table'], $options); + } } return $resultSet; } diff --git a/library/think/db/exception/DataNotFoundException.php b/library/think/db/exception/DataNotFoundException.php index ac0f416d..efd66d3b 100644 --- a/library/think/db/exception/DataNotFoundException.php +++ b/library/think/db/exception/DataNotFoundException.php @@ -31,5 +31,13 @@ class DataNotFoundException extends DbException $this->setData('Database Config', $config); } - + /** + * 获取数据表名 + * @access public + * @return string + */ + public function getTable() + { + return $this->table; + } } diff --git a/library/think/db/exception/ModelNotFoundException.php b/library/think/db/exception/ModelNotFoundException.php index aba7187b..69b70965 100644 --- a/library/think/db/exception/ModelNotFoundException.php +++ b/library/think/db/exception/ModelNotFoundException.php @@ -30,6 +30,11 @@ class ModelNotFoundException extends DbException $this->setData('Database Config', $config); } + /** + * 获取模型类名 + * @access public + * @return string + */ public function getModel() { return $this->model; diff --git a/library/think/exception/ClassNotFoundException.php b/library/think/exception/ClassNotFoundException.php index a02d1cb1..7160f86b 100644 --- a/library/think/exception/ClassNotFoundException.php +++ b/library/think/exception/ClassNotFoundException.php @@ -13,4 +13,20 @@ namespace think\exception; class ClassNotFoundException extends \RuntimeException { + protected $class; + public function __construct($message,$class='') + { + $this->message = $message; + $this->class = $class; + } + + /** + * 获取类名 + * @access public + * @return string + */ + public function getClass() + { + return $this->class; + } } \ No newline at end of file diff --git a/library/think/exception/TemplateNotFoundException.php b/library/think/exception/TemplateNotFoundException.php index 5632a747..b9d5294e 100644 --- a/library/think/exception/TemplateNotFoundException.php +++ b/library/think/exception/TemplateNotFoundException.php @@ -13,4 +13,21 @@ namespace think\exception; class TemplateNotFoundException extends \RuntimeException { + protected $template; + + public function __construct($message,$template='') + { + $this->message = $message; + $this->template = $template; + } + + /** + * 获取模板文件 + * @access public + * @return string + */ + public function getTemplate() + { + return $this->template; + } } \ No newline at end of file diff --git a/library/think/exception/ValidateException.php b/library/think/exception/ValidateException.php index 34cdae83..6f1cd4d1 100644 --- a/library/think/exception/ValidateException.php +++ b/library/think/exception/ValidateException.php @@ -13,4 +13,20 @@ namespace think\exception; class ValidateException extends \RuntimeException { + protected $error; + + public function __construct($error) + { + $this->error = $error; + } + + /** + * 获取验证错误信息 + * @access public + * @return array|string + */ + public function getError() + { + return $this->error; + } } \ No newline at end of file diff --git a/library/think/view/driver/Php.php b/library/think/view/driver/Php.php index de841824..7db03fdb 100644 --- a/library/think/view/driver/Php.php +++ b/library/think/view/driver/Php.php @@ -62,7 +62,7 @@ class Php } // 模板不存在 抛出异常 if (!is_file($template)) { - throw new TemplateNotFoundException('template file not exists:' . $template); + throw new TemplateNotFoundException('template file not exists:' . $template, $template); } // 记录视图信息 APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info'); diff --git a/library/think/view/driver/Think.php b/library/think/view/driver/Think.php index 4a14fc8f..906fcf10 100644 --- a/library/think/view/driver/Think.php +++ b/library/think/view/driver/Think.php @@ -72,7 +72,7 @@ class Think } // 模板不存在 抛出异常 if (!is_file($template)) { - throw new TemplateNotFoundException('template file not exists:' . $template); + throw new TemplateNotFoundException('template file not exists:' . $template, $template); } // 记录视图信息 APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info');