diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 9550b556..787edc07 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -79,22 +79,23 @@ class BelongsToMany extends Relation { return [ $this->localKey => $data[$this->localKey], - $this->foreignKey => $data[$this->foreignKey] + $this->foreignKey => $data[$this->foreignKey], ]; } /** * 实例化中间表模型 - * @param $data + * @param array $data + * @param bool $isUpdate * @return Pivot * @throws Exception */ - protected function newPivot($data = []) + protected function newPivot($data = [], $isUpdate = false) { $class = $this->pivotName ?: '\\think\\model\\Pivot'; $pivot = new $class($data, $this->parent, $this->middle); if ($pivot instanceof Pivot) { - return $pivot; + return $isUpdate ? $pivot->isUpdate(true, $this->getUpdateWhere($data)) : $pivot; } else { throw new Exception('pivot model must extends: \think\model\Pivot'); } @@ -117,7 +118,7 @@ class BelongsToMany extends Relation } } } - $model->setRelation('pivot', $this->newPivot($pivot)->isUpdate(true, $this->getUpdateWhere($pivot))); + $model->setRelation('pivot', $this->newPivot($pivot, true)); } } @@ -383,7 +384,7 @@ class BelongsToMany extends Relation } } } - $set->setRelation('pivot', $this->newPivot($pivot)->isUpdate(true, $this->getUpdateWhere($pivot))); + $set->setRelation('pivot', $this->newPivot($pivot, true)); $data[$pivot[$this->localKey]][] = $set; } return $data; @@ -486,7 +487,7 @@ class BelongsToMany extends Relation foreach ($ids as $id) { $pivot[$this->foreignKey] = $id; $this->pivot->insert($pivot, true); - $result[] = $this->newPivot($pivot)->isUpdate(true, $this->getUpdateWhere($pivot)); + $result[] = $this->newPivot($pivot, true); } if (count($result) == 1) { // 返回中间表模型对象