mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
改进Model类saveall方法的数据验证机制
This commit is contained in:
@@ -734,6 +734,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function saveAll($dataSet, $replace = true)
|
public function saveAll($dataSet, $replace = true)
|
||||||
{
|
{
|
||||||
|
if ($this->validate) {
|
||||||
|
// 数据批量验证
|
||||||
|
foreach ($dataSet as $data) {
|
||||||
|
if (!$this->validateData($data)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$result = [];
|
$result = [];
|
||||||
$db = $this->db();
|
$db = $this->db();
|
||||||
$db->startTrans();
|
$db->startTrans();
|
||||||
@@ -744,9 +753,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
}
|
}
|
||||||
foreach ($dataSet as $key => $data) {
|
foreach ($dataSet as $key => $data) {
|
||||||
if (!empty($auto) && isset($data[$pk])) {
|
if (!empty($auto) && isset($data[$pk])) {
|
||||||
$result[$key] = self::update($data, [], $this->validate, true);
|
$result[$key] = self::update($data);
|
||||||
} else {
|
} else {
|
||||||
$result[$key] = self::create($data, $this->validate, true);
|
$result[$key] = self::create($data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$db->commit();
|
$db->commit();
|
||||||
@@ -960,19 +969,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
* 写入数据
|
* 写入数据
|
||||||
* @access public
|
* @access public
|
||||||
* @param array $data 数据数组
|
* @param array $data 数据数组
|
||||||
* @param mixed $validate 数据验证规则
|
* @return $this
|
||||||
* @param boolean $exception 验证失败是否抛出异常
|
|
||||||
* @return $this|false
|
|
||||||
*/
|
*/
|
||||||
public static function create($data = [], $validate = null, $exception = false)
|
public static function create($data = [])
|
||||||
{
|
{
|
||||||
$model = new static();
|
$model = new static();
|
||||||
$result = $model
|
$model->isUpdate(false)->save($data, []);
|
||||||
->validate($validate)
|
return $model;
|
||||||
->validateFailException($exception)
|
|
||||||
->isUpdate(false)
|
|
||||||
->save($data, []);
|
|
||||||
return false === $result ? false : $model;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -980,19 +983,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
* @access public
|
* @access public
|
||||||
* @param array $data 数据数组
|
* @param array $data 数据数组
|
||||||
* @param array $where 更新条件
|
* @param array $where 更新条件
|
||||||
* @param mixed $validate 数据验证规则
|
* @return $this
|
||||||
* @param boolean $exception 验证失败是否抛出异常
|
|
||||||
* @return $this|false
|
|
||||||
*/
|
*/
|
||||||
public static function update($data = [], $where = [], $validate = null, $exception = false)
|
public static function update($data = [], $where = [])
|
||||||
{
|
{
|
||||||
$model = new static();
|
$model = new static();
|
||||||
$result = $model
|
$result = $model->isUpdate(true)->save($data, $where);
|
||||||
->validate($validate)
|
return $model;
|
||||||
->validateFailException($exception)
|
|
||||||
->isUpdate(true)
|
|
||||||
->save($data, $where);
|
|
||||||
return false === $result ? false : $model;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -13,20 +13,20 @@ namespace think\exception;
|
|||||||
|
|
||||||
class ClassNotFoundException extends \RuntimeException
|
class ClassNotFoundException extends \RuntimeException
|
||||||
{
|
{
|
||||||
protected $class;
|
protected $class;
|
||||||
public function __construct($message,$class='')
|
public function __construct($message, $class = '')
|
||||||
{
|
{
|
||||||
$this->message = $message;
|
$this->message = $message;
|
||||||
$this->class = $class;
|
$this->class = $class;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取类名
|
* 获取类名
|
||||||
* @access public
|
* @access public
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getClass()
|
public function getClass()
|
||||||
{
|
{
|
||||||
return $this->class;
|
return $this->class;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,24 +20,23 @@ class DbException extends Exception
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* DbException constructor.
|
* DbException constructor.
|
||||||
* @param string $message
|
* @param string $message
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param int $code
|
* @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->message = $message;
|
||||||
$this->code = $code;
|
$this->code = $code;
|
||||||
|
|
||||||
$this->setData('Database Status', [
|
$this->setData('Database Status', [
|
||||||
'Error Code' => $code,
|
'Error Code' => $code,
|
||||||
'Error Message' => $message,
|
'Error Message' => $message,
|
||||||
'Error SQL' => $sql
|
'Error SQL' => $sql,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->setData('Database Config', $config);
|
$this->setData('Database Config', $config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@
|
|||||||
|
|
||||||
namespace think\exception;
|
namespace think\exception;
|
||||||
|
|
||||||
|
|
||||||
class HttpException extends \RuntimeException
|
class HttpException extends \RuntimeException
|
||||||
{
|
{
|
||||||
private $statusCode;
|
private $statusCode;
|
||||||
|
|||||||
@@ -11,7 +11,6 @@
|
|||||||
|
|
||||||
namespace think\exception;
|
namespace think\exception;
|
||||||
|
|
||||||
|
|
||||||
use think\Response;
|
use think\Response;
|
||||||
|
|
||||||
class HttpResponseException extends \RuntimeException
|
class HttpResponseException extends \RuntimeException
|
||||||
@@ -31,5 +30,4 @@ class HttpResponseException extends \RuntimeException
|
|||||||
return $this->response;
|
return $this->response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -22,18 +22,18 @@ class PDOException extends DbException
|
|||||||
/**
|
/**
|
||||||
* PDOException constructor.
|
* PDOException constructor.
|
||||||
* @param \PDOException $exception
|
* @param \PDOException $exception
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param int $code
|
* @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;
|
$error = $exception->errorInfo;
|
||||||
|
|
||||||
$this->setData('PDO Error Info', [
|
$this->setData('PDO Error Info', [
|
||||||
'SQLSTATE' => $error[0],
|
'SQLSTATE' => $error[0],
|
||||||
'Driver Error Code' => $error[1],
|
'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);
|
parent::__construct($exception->getMessage(), $config, $sql, $code);
|
||||||
|
|||||||
@@ -13,21 +13,21 @@ namespace think\exception;
|
|||||||
|
|
||||||
class TemplateNotFoundException extends \RuntimeException
|
class TemplateNotFoundException extends \RuntimeException
|
||||||
{
|
{
|
||||||
protected $template;
|
protected $template;
|
||||||
|
|
||||||
public function __construct($message,$template='')
|
public function __construct($message, $template = '')
|
||||||
{
|
{
|
||||||
$this->message = $message;
|
$this->message = $message;
|
||||||
$this->template = $template;
|
$this->template = $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取模板文件
|
* 获取模板文件
|
||||||
* @access public
|
* @access public
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getTemplate()
|
public function getTemplate()
|
||||||
{
|
{
|
||||||
return $this->template;
|
return $this->template;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,6 @@
|
|||||||
|
|
||||||
namespace think\exception;
|
namespace think\exception;
|
||||||
|
|
||||||
|
|
||||||
class ThrowableError extends \ErrorException
|
class ThrowableError extends \ErrorException
|
||||||
{
|
{
|
||||||
public function __construct(\Throwable $e)
|
public function __construct(\Throwable $e)
|
||||||
@@ -36,7 +35,6 @@ class ThrowableError extends \ErrorException
|
|||||||
$e->getLine()
|
$e->getLine()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
$this->setTrace($e->getTrace());
|
$this->setTrace($e->getTrace());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user