Skip to content

Commit

Permalink
改进saveall方法对多主键的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Apr 20, 2023
1 parent 668240e commit 5535503
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,15 +781,20 @@ public function saveAll(iterable $dataSet, bool $replace = true): Collection
$result = $db->transaction(function () use ($replace, $dataSet) {
$pk = $this->getPk();

if (is_string($pk) && $replace) {
$auto = true;
}

$result = [];
$suffix = $this->getSuffix();

foreach ($dataSet as $key => $data) {
if ($this->exists || (!empty($auto) && isset($data[$pk]))) {
if ($replace) {
$exists = true;
foreach ((array) $pk as $field) {
if (!isset($data[$field])) {
$exists = false;
}
}
}

if ($replace && !empty($exists)) {
$result[$key] = static::update($data, [], [], $suffix);
} else {
$result[$key] = static::create($data, $this->field, $this->replace, $suffix);
Expand Down

0 comments on commit 5535503

Please sign in to comment.