From 3454e8a13c9b11add0fcd2ac4d966ab875ccdab2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 20 Aug 2016 14:40:31 +0800 Subject: [PATCH] =?UTF-8?q?model=E7=B1=BBsaveall=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=AF=86=E5=88=AB=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index cb7bd21f..e15b37d6 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -729,16 +729,21 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * 保存多个数据到当前数据对象 * @access public * @param array $dataSet 数据 + * @param boolean $replace 是否自动识别更新和写入 * @return array|false */ - public function saveAll($dataSet) + public function saveAll($dataSet, $replace = true) { $result = []; $db = $this->db(); $db->startTrans(); try { foreach ($dataSet as $key => $data) { - $result[$key] = self::create($data); + if ($replace && isset($data[$this->getPk()])) { + $result[$key] = self::update($data); + } else { + $result[$key] = self::create($data); + } } $db->commit(); return $result;