From 85204dfaffb9d17ed0d6817ac089e63783f4223e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 19 Sep 2016 14:48:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=81=9A=E5=90=88=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E7=9A=84save=E6=96=B9=E6=B3=95=20=E4=B8=BB=E9=94=AE?= =?UTF-8?q?=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/Merge.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/library/think/model/Merge.php b/library/think/model/Merge.php index 405c6bcc..566cf077 100644 --- a/library/think/model/Merge.php +++ b/library/think/model/Merge.php @@ -233,16 +233,24 @@ class Merge extends Model if ($result) { $insertId = $db->getLastInsID($sequence); // 写入外键数据 + $pk = $this->getPk(); if ($insertId) { + if (is_string($pk)) { + $this->data[$pk] = $insertId; + } $this->data[$this->fk] = $insertId; } // 写入附表数据 + $source = $this->data; + if ($insertId && is_string($pk) && isset($source[$pk])) { + unset($source[$pk]); + } foreach (static::$relationModel as $key => $model) { $name = is_int($key) ? $model : $key; $table = is_int($key) ? $db->getTable($model) : $model; // 处理关联模型数据 - $data = $this->parseData($name, $this->data, true); + $data = $this->parseData($name, $source, true); $query = clone $db; $query->table($table)->strict(false)->insert($data); }