改进append方法支持一对一关联的bind设置

This commit is contained in:
thinkphp
2017-09-15 17:32:50 +08:00
parent 8d39aa9b05
commit 2a2b3882af
4 changed files with 42 additions and 10 deletions

View File

@@ -825,11 +825,31 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 追加关联对象属性 // 追加关联对象属性
$relation = $this->getAttr($key); $relation = $this->getAttr($key);
$item[$key] = $relation->append([$attr])->toArray(); $item[$key] = $relation->append([$attr])->toArray();
} else {
$relation = Loader::parseName($name, 1, false);
if (method_exists($this, $relation)) {
$modelRelation = $this->$relation();
$value = $this->getRelationData($modelRelation);
if (method_exists($modelRelation, 'getBindAttr')) {
$bindAttr = $modelRelation->getBindAttr();
foreach ($bindAttr as $key => $attr) {
$key = is_numeric($key) ? $attr : $key;
if (isset($this->data[$key])) {
throw new Exception('bind attr has exists:' . $key);
} else {
$item[$key] = $value ? $value->$attr : null;
}
}
} else {
$item[$name] = $value;
}
} else { } else {
$item[$name] = $this->getAttr($name); $item[$name] = $this->getAttr($name);
} }
} }
} }
}
return !empty($item) ? $item : []; return !empty($item) ? $item : [];
} }

View File

@@ -146,13 +146,13 @@ class BelongsTo extends OneToOne
if (!empty($this->bindAttr)) { if (!empty($this->bindAttr)) {
// 绑定关联属性 // 绑定关联属性
$this->bindAttr($relationModel, $result, $this->bindAttr); $this->bindAttr($relationModel, $result, $this->bindAttr);
} } else {
// 设置关联属性 // 设置关联属性
$result->setRelation($attr, $relationModel); $result->setRelation($attr, $relationModel);
} }
} }
} }
}
/** /**
* 预载入关联查询(数据) * 预载入关联查询(数据)
@@ -179,10 +179,11 @@ class BelongsTo extends OneToOne
if (!empty($this->bindAttr)) { if (!empty($this->bindAttr)) {
// 绑定关联属性 // 绑定关联属性
$this->bindAttr($relationModel, $result, $this->bindAttr); $this->bindAttr($relationModel, $result, $this->bindAttr);
} } else {
// 设置关联属性 // 设置关联属性
$result->setRelation(Loader::parseName($relation), $relationModel); $result->setRelation(Loader::parseName($relation), $relationModel);
} }
}
/** /**
* 添加关联数据 * 添加关联数据

View File

@@ -147,12 +147,13 @@ class HasOne extends OneToOne
if (!empty($this->bindAttr)) { if (!empty($this->bindAttr)) {
// 绑定关联属性 // 绑定关联属性
$this->bindAttr($relationModel, $result, $this->bindAttr); $this->bindAttr($relationModel, $result, $this->bindAttr);
} } else {
// 设置关联属性 // 设置关联属性
$result->setRelation($attr, $relationModel); $result->setRelation($attr, $relationModel);
} }
} }
} }
}
/** /**
* 预载入关联查询(数据) * 预载入关联查询(数据)
@@ -180,9 +181,9 @@ class HasOne extends OneToOne
if (!empty($this->bindAttr)) { if (!empty($this->bindAttr)) {
// 绑定关联属性 // 绑定关联属性
$this->bindAttr($relationModel, $result, $this->bindAttr); $this->bindAttr($relationModel, $result, $this->bindAttr);
} } else {
$result->setRelation(Loader::parseName($relation), $relationModel); $result->setRelation(Loader::parseName($relation), $relationModel);
} }
}
} }

View File

@@ -214,6 +214,16 @@ abstract class OneToOne extends Relation
return $this; return $this;
} }
/**
* 获取绑定属性
* @access public
* @return array
*/
public function getBindAttr()
{
return $this->bindAttr;
}
/** /**
* 关联统计 * 关联统计
* @access public * @access public