From b599829e75902849bca19a848a22a48385c0da6b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 20 Apr 2016 08:33:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BRelation=E7=B1=BB=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=85=B3=E8=81=94=E7=9A=84=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 | 12 ++--- library/think/db/Query.php | 4 +- library/think/model/Relation.php | 77 ++++++++++++++++++++++++++------ 3 files changed, 73 insertions(+), 20 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index d2547daf..f2cde8d9 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -66,6 +66,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $isUpdate = false; // 当前执行的关联对象 protected $relation; + // 当前模型的父模型 + protected $parent; /** * 初始化过的模型. @@ -329,7 +331,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param array $where 更新条件 * @return integer */ - public function save($data = [], $where = []) + public function save($data = [], $where = [], $getInsertId = true) { if (!empty($data)) { // 数据对象赋值 @@ -392,7 +394,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $result = self::db()->insert($this->data); // 获取自动增长主键 - if ($result) { + if ($result && $getInsertId) { $insertId = self::db()->getLastInsID(); if (is_string($this->pk) && $insertId) { $this->data[$this->pk] = $insertId; @@ -420,7 +422,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function saveAll($dataSet) { foreach ($dataSet as $data) { - $result = $this->save($data); + $result = $this->isUpdate(false)->save($data, [], false); } return $result; } @@ -858,10 +860,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 初始化数据库对象 - * @access protected + * @access public * @return \think\db\Driver */ - protected static function db() + public static function db() { $model = get_called_class(); diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 1c3e5fe4..7195e6c4 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -850,8 +850,8 @@ class Query $this->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName); } // 预载入封装 - $table = $model::getTableName(); - $name = strtolower(basename(str_replace('\\', '/', $model))); + $table = $info['model']::getTableName(); + $name = strtolower(basename(str_replace('\\', '/', $info['model']))); $this->via($name); $this->join($table . ' ' . $name, $joinName . '.' . $info['localKey'] . '=' . $name . '.' . $info['foreignKey'])->field(true, false, $table, $name, $name . '__'); if ($closure) { diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index 907ac538..beffdcd2 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -41,6 +41,7 @@ class Relation public function __construct($model) { $this->parent = $model; + $this->db = $model->db(); } /** @@ -62,17 +63,17 @@ class Relation } // 获取关联数据 - public function getRelation($relation) + public function getRelation($name) { // 执行关联定义方法 - $model = $this->parent->$relation(); + $relation = $this->parent->$name(); $foreignKey = $this->foreignKey; $localKey = $this->localKey; // 判断关联类型执行查询 switch ($this->type) { case self::HAS_ONE: case self::BELONGS_TO: - $result = $model::where($foreignKey, $this->parent->$localKey)->find(); + $result = $relation->where($foreignKey, $this->parent->$localKey)->find(); if (false === $result) { $class = $this->model; $result = new $class; @@ -80,7 +81,7 @@ class Relation } break; case self::HAS_MANY: - $result = $model::where($foreignKey, $this->parent->$localKey)->select(); + $result = $relation->where($foreignKey, $this->parent->$localKey)->select(); break; case self::BELONGS_TO_MANY: // 关联查询 @@ -147,7 +148,7 @@ class Relation } if (!empty($range)) { - $data = $this->eagerlyOneToMany($model, [$foreignKey => ['in', $range]], $relation, $subRelation); + $data = $this->eagerlyOneToMany($this->model, [$foreignKey => ['in', $range]], $relation, $subRelation); // 关联数据封装 foreach ($resultSet as $result) { @@ -171,7 +172,7 @@ class Relation if (!empty($range)) { // 查询关联数据 - $data = $this->eagerlyManyToMany($model, ['pivot.' . $foreignKey => ['in', $range]], $relation, $subRelation); + $data = $this->eagerlyManyToMany($this->model, ['pivot.' . $foreignKey => ['in', $range]], $relation, $subRelation); // 关联数据封装 foreach ($resultSet as $result) { @@ -213,7 +214,7 @@ class Relation case self::HAS_ONE: case self::BELONGS_TO: // 模型关联组装 - $this->match($model, $relation, $result); + $this->match($this->model, $relation, $result); break; case self::HAS_MANY: if (isset($result->$localKey)) { @@ -230,7 +231,7 @@ class Relation if (isset($result->$pk)) { $pk = $result->$pk; // 查询管理数据 - $data = $this->eagerlyManyToMany($model, ['pivot.' . $foreignKey => $pk], $relation, $subRelation); + $data = $this->eagerlyManyToMany($this->model, ['pivot.' . $foreignKey => $pk], $relation, $subRelation); // 关联数据封装 if (!isset($data[$pk])) { @@ -287,7 +288,7 @@ class Relation { $foreignKey = $this->foreignKey; // 预载入关联查询 支持嵌套预载入 - $list = $model::where($where)->with($subRelation)->select(); + $list = $model->where($where)->with($subRelation)->select(); // 组装模型数据 $data = []; @@ -348,7 +349,7 @@ class Relation $this->localKey = $localKey; // 返回关联的模型对象 - return new $model; + return $this; } /** @@ -368,7 +369,7 @@ class Relation $this->localKey = $localKey; // 返回关联的模型对象 - return new $model; + return $this; } /** @@ -388,7 +389,7 @@ class Relation $this->localKey = $localKey; // 返回关联的模型对象 - return new $model; + return $this; } /** @@ -410,7 +411,7 @@ class Relation $this->middle = $table; // 返回关联的模型对象 - return new $model; + return $this; } /** @@ -434,4 +435,54 @@ class Relation ->where($condition); } + /** + * 保存当前关联数据对象 + * @access public + * @param array $data 数据 + * @return integer + */ + public function save($data, $pivot = []) + { + // 判断关联类型执行查询 + switch ($this->type) { + case self::HAS_ONE: + case self::BELONGS_TO: + case self::HAS_MANY: + $data[$this->foreignKey] = $this->parent->{$this->localKey}; + break; + case self::BELONGS_TO_MANY: + break; + } + $model = new $this->model; + return $model->save($data); + } + + /** + * 保存当前关联数据对象 + * @access public + * @param array $data 数据 + * @return integer + */ + public function saveAll($dataSet, $pivot = []) + { + foreach ($dataSet as $data) { + // 判断关联类型执行查询 + switch ($this->type) { + case self::HAS_MANY: + $data[$this->foreignKey] = $this->parent->{$this->localKey}; + break; + case self::BELONGS_TO_MANY: + break; + } + } + $model = new $this->model; + return $model->saveAll($dataSet); + } + + public function __call($method, $args) + { + $model = new $this->model; + return call_user_func_array([$model->db(), $method], $args); + } + }