mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
一对一关联定义支持绑定关联模型属性到当前模型 在定义关联的时候使用bind方法
This commit is contained in:
@@ -62,4 +62,5 @@ return [
|
|||||||
'sae mc write error' => 'SAE mc 写入错误',
|
'sae mc write error' => 'SAE mc 写入错误',
|
||||||
'route name not exists' => '路由标识不存在(或参数不够)',
|
'route name not exists' => '路由标识不存在(或参数不够)',
|
||||||
'invalid request' => '非法请求',
|
'invalid request' => '非法请求',
|
||||||
|
'bind attr has exists' => '模型的属性已经存在',
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -85,7 +85,13 @@ class BelongsTo extends OneToOne
|
|||||||
if (!isset($data[$result->$foreignKey])) {
|
if (!isset($data[$result->$foreignKey])) {
|
||||||
$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])) {
|
if (!isset($data[$result->$foreignKey])) {
|
||||||
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -111,7 +111,13 @@ class HasOne extends OneToOne
|
|||||||
if (!isset($data[$result->$localKey])) {
|
if (!isset($data[$result->$localKey])) {
|
||||||
$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])) {
|
if (!isset($data[$result->$localKey])) {
|
||||||
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
namespace think\model\relation;
|
namespace think\model\relation;
|
||||||
|
|
||||||
use think\db\Query;
|
use think\db\Query;
|
||||||
|
use think\Exception;
|
||||||
use think\Loader;
|
use think\Loader;
|
||||||
use think\Model;
|
use think\Model;
|
||||||
use think\model\Relation;
|
use think\model\Relation;
|
||||||
@@ -21,6 +22,8 @@ abstract class OneToOne extends Relation
|
|||||||
{
|
{
|
||||||
// 预载入方式
|
// 预载入方式
|
||||||
protected $eagerlyType = 0;
|
protected $eagerlyType = 0;
|
||||||
|
// 要绑定的属性
|
||||||
|
protected $bindAttr = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 预载入关联查询(JOIN方式)
|
* 预载入关联查询(JOIN方式)
|
||||||
@@ -161,6 +164,21 @@ abstract class OneToOne extends Relation
|
|||||||
return $this->eagerlyType;
|
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
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user