From a65cb2e17201df4accba35b1bc5a934877bd14ba Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 20 Aug 2016 16:56:01 +0800 Subject: [PATCH] =?UTF-8?q?saveall=E6=96=B9=E6=B3=95=E6=94=B9=E8=BF=9B?= =?UTF-8?q?=E5=AF=B9=E9=AA=8C=E8=AF=81=E9=94=99=E8=AF=AF=E7=9A=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=20=E4=BC=9A=E6=8A=9B=E5=87=BA=E5=BC=82=E5=B8=B8=20?= =?UTF-8?q?=E5=B9=B6=E5=9B=9E=E6=BB=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 20f98266..fa743208 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -744,9 +744,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); + $result[$key] = self::update($data, [], $this->validate, true); } else { - $result[$key] = self::create($data, $this->validate); + $result[$key] = self::create($data, $this->validate, true); } } $db->commit(); @@ -961,13 +961,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param array $data 数据数组 * @param mixed $validate 数据验证规则 - * @return $this + * @param boolean $exception 验证失败是否抛出异常 + * @return $this|false */ - public static function create($data = [], $validate = null) + public static function create($data = [], $validate = null, $exception = false) { - $model = new static(); - $model->validate($validate)->isUpdate(false)->save($data, []); - return $model; + $model = new static(); + $result = $model + ->validate($validate) + ->validateFailException($exception) + ->isUpdate(false) + ->save($data, []); + return false === $result ? false : $model; } /** @@ -976,13 +981,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param array $data 数据数组 * @param array $where 更新条件 * @param mixed $validate 数据验证规则 - * @return $this + * @param boolean $exception 验证失败是否抛出异常 + * @return $this|false */ - public static function update($data = [], $where = [], $validate = null) + public static function update($data = [], $where = [], $validate = null, $exception = false) { - $model = new static(); - $model->validate($validate)->isUpdate(true)->save($data, $where); - return $model; + $model = new static(); + $result = $model + ->validate($validate) + ->validateFailException($exception) + ->isUpdate(true) + ->save($data, $where); + return false === $result ? false : $model; } /**