From aa585a60b2e76233dab1d53e538620c6cb70c917 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 19 Apr 2016 22:16:28 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BBfield=E6=96=B9=E6=B3=95=E6=9B=B4?= =?UTF-8?q?=E5=90=8D=E4=B8=BAallowField=EF=BC=8C=E5=A2=9E=E5=8A=A0saveAll?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=89=B9=E9=87=8F=E6=96=B0=E5=A2=9E=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=20=E6=94=B9=E8=BF=9B=E4=B8=80=E5=AF=B9=E4=B8=80?= =?UTF-8?q?=E7=9A=84=E5=85=B3=E8=81=94=E6=96=B0=E5=A2=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 16 +++++++++++++++- library/think/model/Relation.php | 11 +++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 4aa4c4e5..d2547daf 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -411,13 +411,27 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $result; } + /** + * 保存多个数据到当前数据对象 + * @access public + * @param array $data 数据 + * @return integer + */ + public function saveAll($dataSet) + { + foreach ($dataSet as $data) { + $result = $this->save($data); + } + return $result; + } + /** * 设置允许写入的字段 * @access public * @param bool $update * @return $this */ - public function field($field) + public function allowField($field) { $this->field = $field; return $this; diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index 62def09f..bd529ccd 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -73,6 +73,13 @@ class Relation case self::HAS_ONE: case self::BELONGS_TO: $result = $db->find(); + if (false === $result) { + $class = $this->model; + $result = new $class; + $foreignKey = $this->foreignKey; + $localKey = $this->localKey; + $result->$foreignKey = $this->parent->$localKey; + } break; case self::HAS_MANY: case self::BELONGS_TO_MANY: @@ -240,7 +247,7 @@ class Relation // 重新组装模型数据 foreach ($result->toArray() as $key => $val) { if (strpos($key, '__')) { - list($name, $attr) = explode('__', $key,2); + list($name, $attr) = explode('__', $key, 2); if ($name == $modelName) { $list[$name][$attr] = $val; unset($result->$key); @@ -300,7 +307,7 @@ class Relation $pivot = []; foreach ($set->toArray() as $key => $val) { if (strpos($key, '__')) { - list($name, $attr) = explode('__', $key,2); + list($name, $attr) = explode('__', $key, 2); if ('pivot' == $name) { $pivot[$attr] = $val; unset($set->$key);