From 8a564092e4c62e6dd8354682fd879d5a82bce82e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 12 Apr 2016 18:26:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E9=A2=84=E8=BD=BD?= =?UTF-8?q?=E5=85=A5=E7=9A=84=E6=95=B0=E6=8D=AE=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 45 ++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 6e7b3ffc..cdadadd3 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -815,15 +815,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $range = []; foreach ($resultSet as $result) { // 获取关联外键列表 - $range[] = $result->$localKey; + if (isset($result->$localKey)) { + $range[] = $result->$localKey; + } } - $where[$foreignKey] = ['in', $range]; - $data = $this->modelRelationQuery($model, $where, $relation, $subRelation); - // 关联数据封装 - foreach ($resultSet as &$result) { - if (isset($data[$result->$localKey])) { - $result->__set($relation, $data[$result->$localKey]); + if (!empty($range)) { + $data = $this->modelRelationQuery($model, [$foreignKey => ['in', $range]], $relation, $subRelation); + + // 关联数据封装 + foreach ($resultSet as &$result) { + if (isset($data[$result->$localKey])) { + $result->__set($relation, $data[$result->$localKey]); + } } } break; @@ -864,12 +868,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess break; case self::HAS_MANY: case self::BELONGS_TO_MANY: - $where[$foreignKey] = $result->$localKey; - $data = $this->modelRelationQuery($model, $resultSet, $where, $relation, $subRelation); + if (isset($result->$localKey)) { + $data = $this->modelRelationQuery($model, $resultSet, [$foreignKey => $result->$localKey], $relation, $subRelation); - // 关联数据封装 - if (isset($data[$result->$localKey])) { - $result->__set($relation, $data[$result->$localKey]); + // 关联数据封装 + if (isset($data[$result->$localKey])) { + $result->__set($relation, $data[$result->$localKey]); + } } break; } @@ -949,11 +954,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function hasOne($model, $foreignKey = '', $localKey = '') { - $model = $this->parseModel($model); + // 记录当前关联信息 $localKey = $localKey ?: $this->pk; $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $this->relation = [self::HAS_ONE, $foreignKey, $localKey]; + $model = $this->parseModel($model); if (!$this->eagerly && isset($this->data[$localKey])) { // 关联查询封装 return $model::where($foreignKey, $this->data[$localKey]); @@ -967,17 +973,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * BELONGS TO 关联定义 * @access public * @param string $model 模型名 - * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 + * @param string $foreignKey 关联外键 * @return mixed */ public function belongsTo($model, $localKey = '', $foreignKey = '') { - $model = $this->parseModel($model); + // 记录当前关联信息 $foreignKey = $foreignKey ?: $this->pk; $localKey = $localKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id'; $this->relation = [self::BELONGS_TO, $foreignKey, $localKey]; + $model = $this->parseModel($model); if (!$this->eagerly && isset($this->data[$localKey])) { // 关联查询封装 return $model::where($foreignKey, $this->data[$localKey]); @@ -996,11 +1003,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function hasMany($model, $foreignKey = '', $localKey = '') { - $model = $this->parseModel($model); + // 记录当前关联信息 $localKey = $localKey ?: $this->pk; $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $this->relation = [self::HAS_MANY, $foreignKey, $localKey]; + $model = $this->parseModel($model); if (!$this->eagerly && isset($this->data[$localKey])) { // 关联查询封装 return $model::where($foreignKey, $this->data[$localKey]); @@ -1013,17 +1021,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * BELONGS TO MANY 关联定义 * @access public * @param string $model 模型名 - * @param string $foreignKey 关联外键 * @param string $localKey 关联主键 + * @param string $foreignKey 关联外键 * @return mixed */ public function belongsToMany($model, $localKey = '', $foreignKey = '') { - $model = $this->parseModel($model); + // 记录当前关联信息 $foreignKey = $foreignKey ?: $this->pk; $localKey = $localKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id'; $this->relation = [self::BELONGS_TO_MANY, $foreignKey, $localKey]; + $model = $this->parseModel($model); if (!$this->eagerly && isset($this->data[$localKey])) { // 关联查询封装 return $model::where($foreignKey, $this->data[$localKey]);