diff --git a/lang/zh-cn.php b/lang/zh-cn.php index db43a1c4..b3fef357 100644 --- a/lang/zh-cn.php +++ b/lang/zh-cn.php @@ -62,4 +62,5 @@ return [ 'sae mc write error' => 'SAE mc 写入错误', 'route name not exists' => '路由标识不存在(或参数不够)', 'invalid request' => '非法请求', + 'bind attr has exists' => '模型的属性已经存在', ]; diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 7626caf7..b29355a0 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -85,7 +85,13 @@ class BelongsTo extends OneToOne if (!isset($data[$result->$foreignKey])) { $data[$result->$foreignKey] = []; } - $result->setAttr($relation, $this->resultSetBuild($data[$result->$foreignKey], $class)); + $relationModel = $this->resultSetBuild($data[$result->$foreignKey], $class); + if (!empty($this->bindAttr)) { + // 绑定关联属性 + $this->bindAttr($relationModel, $result, $this->bindAttr); + } + // 设置关联属性 + $result->setAttr($relation, $relationModel); } } } @@ -109,7 +115,13 @@ class BelongsTo extends OneToOne if (!isset($data[$result->$foreignKey])) { $data[$result->$foreignKey] = []; } - $result->setAttr($relation, $this->resultSetBuild($data[$result->$foreignKey], $class)); + $relationModel = $this->resultSetBuild($data[$result->$foreignKey], $class); + if (!empty($this->bindAttr)) { + // 绑定关联属性 + $this->bindAttr($relationModel, $result, $this->bindAttr); + } + // 设置关联属性 + $result->setAttr($relation, $relationModel); } } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index e76fbbaf..fbfc8042 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -111,7 +111,13 @@ class HasOne extends OneToOne if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } - $result->setAttr($relation, $this->resultSetBuild($data[$result->$localKey], $class)); + $relationModel = $this->resultSetBuild($data[$result->$localKey], $class); + if (!empty($this->bindAttr)) { + // 绑定关联属性 + $this->bindAttr($relationModel, $result, $this->bindAttr); + } + // 设置关联属性 + $result->setAttr($relation, $relationModel); } } } @@ -135,7 +141,12 @@ class HasOne extends OneToOne if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } - $result->setAttr($relation, $this->resultSetBuild($data[$result->$localKey], $class)); + $relationModel = $this->resultSetBuild($data[$result->$localKey], $class); + if (!empty($this->bindAttr)) { + // 绑定关联属性 + $this->bindAttr($relationModel, $result, $this->bindAttr); + } + $result->setAttr($relation, $relationModel); } } diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 354d718d..fb36591f 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -12,6 +12,7 @@ namespace think\model\relation; use think\db\Query; +use think\Exception; use think\Loader; use think\Model; use think\model\Relation; @@ -21,6 +22,8 @@ abstract class OneToOne extends Relation { // 预载入方式 protected $eagerlyType = 0; + // 要绑定的属性 + protected $bindAttr = []; /** * 预载入关联查询(JOIN方式) @@ -161,6 +164,21 @@ abstract class OneToOne extends Relation return $this->eagerlyType; } + /** + * 绑定关联表的属性到父模型属性 + * @access public + * @param mixed $attr 要绑定的属性列表 + * @return this + */ + public function bind($attr) + { + if (is_string($attr)) { + $attr = explode(',', $attr); + } + $this->bindAttr = $attr; + return $this; + } + /** * 一对一 关联模型预查询拼装 * @access public @@ -181,8 +199,32 @@ abstract class OneToOne extends Relation } } } + if (isset($list[$relation])) { + $relationModel = new $model($list[$relation]); + if (!empty($this->bindAttr)) { + $this->bindAttr($relationModel, $result, $this->bindAttr); + } + } + $result->setAttr($relation, !isset($relationModel) ? null : $relationModel->isUpdate(true)); + } - $result->setAttr($relation, !isset($list[$relation]) ? null : (new $model($list[$relation]))->isUpdate(true)); + /** + * 绑定关联属性到父模型 + * @access protected + * @param Model $model 关联模型对象 + * @param Model $result 父模型对象 + * @param array $bindAttr 绑定属性 + * @return void + */ + protected function bindAttr($model, &$result, $bindAttr) + { + foreach ($bindAttr as $key => $attr) { + if (!isset($result->$attr)) { + $result->setAttr(is_numeric($key) ? $attr : $key, $model->$attr); + } else { + throw new Exception('bind attr has exists:' . $attr); + } + } } /**