From 04cf8a34e37ccf299add21b213076755cd1485c2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 17 Apr 2017 13:40:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Btogether=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BC=A0=E5=85=A5=E5=85=B3=E8=81=94=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 55 ++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index f2acbad5..f949a794 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -894,13 +894,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (!empty($this->relationWrite)) { $relation = []; foreach ($this->relationWrite as $key => $name) { - if (!is_numeric($key)) { - $relation[$key] = []; - foreach ($name as $val) { - if (isset($this->data[$val])) { - $relation[$key][$val] = $this->data[$val]; - unset($this->data[$val]); + if (is_array($name)) { + if (key($name) === 0) { + $relation[$key] = []; + foreach ($name as $val) { + if (isset($this->data[$val])) { + $relation[$key][$val] = $this->data[$val]; + unset($this->data[$val]); + } } + } else { + $relation[$key] = $name; } } elseif (isset($this->relation[$name])) { $relation[$name] = $this->relation[$name]; @@ -943,17 +947,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (empty($data) || (count($data) == 1 && is_string($pk) && isset($data[$pk]))) { // 关联更新 if (isset($relation)) { - foreach ($relation as $name => $val) { - if ($val instanceof Model) { - $val->save(); - } else { - unset($this->data[$name]); - $model = $this->getAttr($name); - if ($model instanceof Model) { - $model->save($val); - } - } - } + $this->autoRelationUpdate($relation); } return 0; } elseif ($this->autoWriteTimestamp && $this->updateTime && !isset($data[$this->updateTime])) { @@ -985,17 +979,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 关联更新 if (isset($relation)) { - foreach ($relation as $name => $val) { - if ($val instanceof Model) { - $val->save(); - } else { - unset($this->data[$name]); - $model = $this->getAttr($name); - if ($model instanceof Model) { - $model->save($val); - } - } - } + $this->autoRelationUpdate($relation); } // 更新回调 @@ -1051,6 +1035,21 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $result; } + protected function autoRelationUpdate($relation) + { + foreach ($relation as $name => $val) { + if ($val instanceof Model) { + $val->save(); + } else { + unset($this->data[$name]); + $model = $this->getAttr($name); + if ($model instanceof Model) { + $model->save($val); + } + } + } + } + /** * 获取变化的数据 并排除只读数据 * @access public