mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-09 16:12:49 +08:00
改进append方法支持一对一关联的bind设置
This commit is contained in:
@@ -826,7 +826,27 @@ 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 {
|
} else {
|
||||||
$item[$name] = $this->getAttr($name);
|
$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 {
|
||||||
|
$item[$name] = $this->getAttr($name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -146,10 +146,10 @@ 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,9 +179,10 @@ 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -147,9 +147,10 @@ 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user