From 308a0b9ca8ffd4c1fc9e6e3fbadc826000b1c9a2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 9 Dec 2016 15:01:34 +0800 Subject: [PATCH] =?UTF-8?q?MorphMany=E5=85=B3=E8=81=94=E5=A2=9E=E5=8A=A0sa?= =?UTF-8?q?ve=E5=92=8Csaveall=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/HasMany.php | 3 +- library/think/model/relation/MorphMany.php | 35 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 298083d4..416c5a5b 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -167,8 +167,7 @@ class HasMany extends Relation { $result = false; foreach ($dataSet as $key => $data) { - $data[$this->foreignKey] = $this->parent->{$this->localKey}; - $result = $this->save($data); + $result = $this->save($data); } return $result; } diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index cfce9b57..5a2e2e9b 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -141,6 +141,41 @@ class MorphMany extends Relation return $data; } + /** + * 保存(新增)当前关联数据对象 + * @access public + * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键 + * @return integer + */ + public function save($data) + { + if ($data instanceof Model) { + $data = $data->getData(); + } + // 保存关联表数据 + $pk = $this->parent->getPk(); + + $data[$this->morphKey] = $this->parent->$pk; + $data[$this->morphType] = $this->type; + $model = new $this->model; + return $model->save($data); + } + + /** + * 批量保存当前关联数据对象 + * @access public + * @param array $dataSet 数据集 + * @return integer + */ + public function saveAll(array $dataSet) + { + $result = false; + foreach ($dataSet as $key => $data) { + $result = $this->save($data); + } + return $result; + } + /** * 执行基础查询(进执行一次) * @access protected