diff --git a/library/think/Model.php b/library/think/Model.php index fa743208..dd246848 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -734,6 +734,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function saveAll($dataSet, $replace = true) { + if ($this->validate) { + // 数据批量验证 + foreach ($dataSet as $data) { + if (!$this->validateData($data)) { + return false; + } + } + } + $result = []; $db = $this->db(); $db->startTrans(); @@ -744,9 +753,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } foreach ($dataSet as $key => $data) { if (!empty($auto) && isset($data[$pk])) { - $result[$key] = self::update($data, [], $this->validate, true); + $result[$key] = self::update($data); } else { - $result[$key] = self::create($data, $this->validate, true); + $result[$key] = self::create($data); } } $db->commit(); @@ -960,19 +969,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * 写入数据 * @access public * @param array $data 数据数组 - * @param mixed $validate 数据验证规则 - * @param boolean $exception 验证失败是否抛出异常 - * @return $this|false + * @return $this */ - public static function create($data = [], $validate = null, $exception = false) + public static function create($data = []) { - $model = new static(); - $result = $model - ->validate($validate) - ->validateFailException($exception) - ->isUpdate(false) - ->save($data, []); - return false === $result ? false : $model; + $model = new static(); + $model->isUpdate(false)->save($data, []); + return $model; } /** @@ -980,19 +983,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param array $data 数据数组 * @param array $where 更新条件 - * @param mixed $validate 数据验证规则 - * @param boolean $exception 验证失败是否抛出异常 - * @return $this|false + * @return $this */ - public static function update($data = [], $where = [], $validate = null, $exception = false) + public static function update($data = [], $where = []) { $model = new static(); - $result = $model - ->validate($validate) - ->validateFailException($exception) - ->isUpdate(true) - ->save($data, $where); - return false === $result ? false : $model; + $result = $model->isUpdate(true)->save($data, $where); + return $model; } /** diff --git a/library/think/exception/ClassNotFoundException.php b/library/think/exception/ClassNotFoundException.php index 7160f86b..0bca424d 100644 --- a/library/think/exception/ClassNotFoundException.php +++ b/library/think/exception/ClassNotFoundException.php @@ -13,20 +13,20 @@ namespace think\exception; class ClassNotFoundException extends \RuntimeException { - protected $class; - public function __construct($message,$class='') - { - $this->message = $message; - $this->class = $class; - } + 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 + public function getClass() + { + return $this->class; + } +} diff --git a/library/think/exception/DbException.php b/library/think/exception/DbException.php index 11c1c932..bd6fb442 100644 --- a/library/think/exception/DbException.php +++ b/library/think/exception/DbException.php @@ -16,28 +16,27 @@ use think\Exception; /** * Database相关异常处理类 */ -class DbException extends Exception +class DbException extends Exception { /** * DbException constructor. - * @param string $message - * @param array $config - * @param string $sql - * @param int $code + * @param string $message + * @param array $config + * @param string $sql + * @param int $code */ - public function __construct($message, Array $config, $sql, $code = 10500) + public function __construct($message, array $config, $sql, $code = 10500) { - $this->message = $message; - $this->code = $code; + $this->message = $message; + $this->code = $code; $this->setData('Database Status', [ 'Error Code' => $code, 'Error Message' => $message, - 'Error SQL' => $sql + 'Error SQL' => $sql, ]); $this->setData('Database Config', $config); } - } diff --git a/library/think/exception/HttpException.php b/library/think/exception/HttpException.php index 99beb002..ef6bbd82 100644 --- a/library/think/exception/HttpException.php +++ b/library/think/exception/HttpException.php @@ -11,7 +11,6 @@ namespace think\exception; - class HttpException extends \RuntimeException { private $statusCode; @@ -34,4 +33,4 @@ class HttpException extends \RuntimeException { return $this->headers; } -} \ No newline at end of file +} diff --git a/library/think/exception/HttpResponseException.php b/library/think/exception/HttpResponseException.php index 249e3da3..ff718f58 100644 --- a/library/think/exception/HttpResponseException.php +++ b/library/think/exception/HttpResponseException.php @@ -11,7 +11,6 @@ namespace think\exception; - use think\Response; class HttpResponseException extends \RuntimeException @@ -31,5 +30,4 @@ class HttpResponseException extends \RuntimeException return $this->response; } - -} \ No newline at end of file +} diff --git a/library/think/exception/PDOException.php b/library/think/exception/PDOException.php index 969a0e6c..abe94d70 100644 --- a/library/think/exception/PDOException.php +++ b/library/think/exception/PDOException.php @@ -17,23 +17,23 @@ use think\exception\DbException; * PDO异常处理类 * 重新封装了系统的\PDOException类 */ -class PDOException extends DbException +class PDOException extends DbException { /** * PDOException constructor. * @param \PDOException $exception - * @param array $config - * @param string $sql - * @param int $code + * @param array $config + * @param string $sql + * @param int $code */ - public function __construct(\PDOException $exception, Array $config, $sql, $code = 10501) + public function __construct(\PDOException $exception, array $config, $sql, $code = 10501) { $error = $exception->errorInfo; $this->setData('PDO Error Info', [ 'SQLSTATE' => $error[0], 'Driver Error Code' => $error[1], - 'Driver Error Message' => isset($error[2]) ? $error[2] : '' + 'Driver Error Message' => isset($error[2]) ? $error[2] : '', ]); parent::__construct($exception->getMessage(), $config, $sql, $code); diff --git a/library/think/exception/TemplateNotFoundException.php b/library/think/exception/TemplateNotFoundException.php index b9d5294e..24105c42 100644 --- a/library/think/exception/TemplateNotFoundException.php +++ b/library/think/exception/TemplateNotFoundException.php @@ -13,21 +13,21 @@ namespace think\exception; class TemplateNotFoundException extends \RuntimeException { - protected $template; + protected $template; - public function __construct($message,$template='') - { - $this->message = $message; - $this->template = $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 + public function getTemplate() + { + return $this->template; + } +} diff --git a/library/think/exception/ThrowableError.php b/library/think/exception/ThrowableError.php index 8ba26ea8..54823d78 100644 --- a/library/think/exception/ThrowableError.php +++ b/library/think/exception/ThrowableError.php @@ -11,7 +11,6 @@ namespace think\exception; - class ThrowableError extends \ErrorException { public function __construct(\Throwable $e) @@ -36,7 +35,6 @@ class ThrowableError extends \ErrorException $e->getLine() ); - $this->setTrace($e->getTrace()); } @@ -46,4 +44,4 @@ class ThrowableError extends \ErrorException $traceReflector->setAccessible(true); $traceReflector->setValue($this, $trace); } -} \ No newline at end of file +}