From 3d587c75c52fb1ce31210fb4362026ca36d921ab Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 15 Nov 2016 19:09:31 +0800 Subject: [PATCH] =?UTF-8?q?model=E7=B1=BB=E7=9A=84saveall=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=94=AF=E6=8C=81=E8=B0=83=E7=94=A8allowField?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=BF=9B=E8=A1=8C=E5=AD=97=E6=AE=B5=E8=BF=87?= =?UTF-8?q?=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index aed24f7a..181ed8e9 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -759,9 +759,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } foreach ($dataSet as $key => $data) { if (!empty($auto) && isset($data[$pk])) { - $result[$key] = self::update($data); + $result[$key] = self::update($data, [], $this->field); } else { - $result[$key] = self::create($data); + $result[$key] = self::create($data, $this->field); } } $db->commit(); @@ -979,12 +979,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 写入数据 * @access public - * @param array $data 数据数组 + * @param array $data 数据数组 + * @param array|true $field 允许字段 * @return $this */ - public static function create($data = []) + public static function create($data = [], $field = null) { $model = new static(); + if (!empty($field)) { + $model->allowField($field); + } $model->isUpdate(false)->save($data, []); return $model; } @@ -992,13 +996,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 更新数据 * @access public - * @param array $data 数据数组 - * @param array $where 更新条件 + * @param array $data 数据数组 + * @param array $where 更新条件 + * @param array|true $field 允许字段 * @return $this */ - public static function update($data = [], $where = []) + public static function update($data = [], $where = [], $field = null) { - $model = new static(); + $model = new static(); + if (!empty($field)) { + $model->allowField($field); + } $result = $model->isUpdate(true)->save($data, $where); return $model; }