From 7ab73d105823c2534717714628cf39a4bdd8c778 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 11 May 2018 14:02:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=A4=9A=E5=AF=B9=E5=A4=9A?= =?UTF-8?q?=E5=85=B3=E8=81=94=E7=9A=84=E4=B8=AD=E9=97=B4=E8=A1=A8=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/BelongsToMany.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) 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) { // 返回中间表模型对象