From baebe7899ebe2ff40cbf23c8c35f42173ef4c2f0 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 17 May 2016 17:53:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BRelation=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/Relation.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index ab605761..a2673237 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -534,9 +534,7 @@ class Relation if (is_array($data)) { // 保存关联表数据 $model = new $this->model; - $model->save($data); - $relationFk = $model->getPk(); - $id = $model->$relationFk; + $id = $model->save($data); } elseif (is_int($data)) { // 根据关联表主键直接写入中间表 $id = $data; @@ -551,7 +549,8 @@ class Relation $pk = $this->parent->getPk(); $pivot[$this->localKey] = $this->parent->$pk; $pivot[$this->foreignKey] = $id; - return Db::table($this->middle)->insert($pivot); + $query = clone $this->parent->db(); + return $query->table($this->middle)->insert($pivot); } else { throw new Exception(' miss relation data'); } @@ -580,7 +579,8 @@ class Relation $pk = $this->parent->getPk(); $pivot[$this->localKey] = $this->parent->$pk; $pivot[$this->foreignKey] = is_array($id) ? ['in', $id] : $id; - Db::table($this->middle)->where($pivot)->delete(); + $query = clone $this->parent->db(); + $query->table($this->middle)->where($pivot)->delete(); // 删除关联表数据 if ($relationDel) { @@ -593,7 +593,7 @@ class Relation { if ($this->model) { $model = new $this->model; - $db = $model->db(); + $db = $model::db(); if (self::HAS_MANY == $this->type && isset($this->parent->{$this->localKey})) { // 关联查询带入关联条件 $db->where($this->foreignKey, $this->parent->{$this->localKey});