model类saveall方法自动识别数据

This commit is contained in:
thinkphp
2016-08-20 14:40:31 +08:00
parent a30b5d7f01
commit 3454e8a13c

View File

@@ -729,17 +729,22 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* 保存多个数据到当前数据对象 * 保存多个数据到当前数据对象
* @access public * @access public
* @param array $dataSet 数据 * @param array $dataSet 数据
* @param boolean $replace 是否自动识别更新和写入
* @return array|false * @return array|false
*/ */
public function saveAll($dataSet) public function saveAll($dataSet, $replace = true)
{ {
$result = []; $result = [];
$db = $this->db(); $db = $this->db();
$db->startTrans(); $db->startTrans();
try { try {
foreach ($dataSet as $key => $data) { foreach ($dataSet as $key => $data) {
if ($replace && isset($data[$this->getPk()])) {
$result[$key] = self::update($data);
} else {
$result[$key] = self::create($data); $result[$key] = self::create($data);
} }
}
$db->commit(); $db->commit();
return $result; return $result;
} catch (\Exception $e) { } catch (\Exception $e) {