mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-09 16:12:49 +08:00
改进append方法支持一对一关联的bind设置
This commit is contained in:
@@ -825,11 +825,31 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
// 追加关联对象属性
|
||||
$relation = $this->getAttr($key);
|
||||
$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 {
|
||||
$item[$name] = $this->getAttr($name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return !empty($item) ? $item : [];
|
||||
}
|
||||
|
||||
|
||||
@@ -146,13 +146,13 @@ class BelongsTo extends OneToOne
|
||||
if (!empty($this->bindAttr)) {
|
||||
// 绑定关联属性
|
||||
$this->bindAttr($relationModel, $result, $this->bindAttr);
|
||||
}
|
||||
|
||||
} else {
|
||||
// 设置关联属性
|
||||
$result->setRelation($attr, $relationModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 预载入关联查询(数据)
|
||||
@@ -179,10 +179,11 @@ class BelongsTo extends OneToOne
|
||||
if (!empty($this->bindAttr)) {
|
||||
// 绑定关联属性
|
||||
$this->bindAttr($relationModel, $result, $this->bindAttr);
|
||||
}
|
||||
} else {
|
||||
// 设置关联属性
|
||||
$result->setRelation(Loader::parseName($relation), $relationModel);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加关联数据
|
||||
|
||||
@@ -147,12 +147,13 @@ class HasOne extends OneToOne
|
||||
if (!empty($this->bindAttr)) {
|
||||
// 绑定关联属性
|
||||
$this->bindAttr($relationModel, $result, $this->bindAttr);
|
||||
}
|
||||
} else {
|
||||
// 设置关联属性
|
||||
$result->setRelation($attr, $relationModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 预载入关联查询(数据)
|
||||
@@ -180,9 +181,9 @@ class HasOne extends OneToOne
|
||||
if (!empty($this->bindAttr)) {
|
||||
// 绑定关联属性
|
||||
$this->bindAttr($relationModel, $result, $this->bindAttr);
|
||||
}
|
||||
|
||||
} else {
|
||||
$result->setRelation(Loader::parseName($relation), $relationModel);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -214,6 +214,16 @@ abstract class OneToOne extends Relation
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取绑定属性
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getBindAttr()
|
||||
{
|
||||
return $this->bindAttr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联统计
|
||||
* @access public
|
||||
|
||||
Reference in New Issue
Block a user