Skip to content

Commit 9788154

Browse files
wdaglbliu21st
authored andcommitted
移植6.x的绑定属性到父模型
1 parent 00d3c4c commit 9788154

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

library/think/model/Collection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ public function load($relation)
3232
return $this;
3333
}
3434

35+
/**
36+
* 绑定(一对一)关联属性到当前模型
37+
* @access protected
38+
* @param string $relation 关联名称
39+
* @param array $attrs 绑定属性
40+
* @return $this
41+
*/
42+
public function bindAttr($relation, array $attrs = [])
43+
{
44+
$this->each(function (Model $model) use ($relation, $attrs) {
45+
$model->bindAttr($relation, $attrs);
46+
});
47+
48+
return $this;
49+
}
50+
3551
/**
3652
* 设置需要隐藏的输出属性
3753
* @access public

library/think/model/concern/RelationShip.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use think\Collection;
1515
use think\db\Query;
16+
use think\Exception;
1617
use think\Loader;
1718
use think\Model;
1819
use think\model\Relation;
@@ -115,6 +116,32 @@ public function setRelation($name, $value, $data = [])
115116
return $this;
116117
}
117118

119+
/**
120+
* 绑定(一对一)关联属性到当前模型
121+
* @access protected
122+
* @param string $relation 关联名称
123+
* @param array $attrs 绑定属性
124+
* @return $this
125+
* @throws Exception
126+
*/
127+
public function bindAttr($relation, array $attrs = [])
128+
{
129+
$relation = $this->getRelation($relation);
130+
131+
foreach ($attrs as $key => $attr) {
132+
$key = is_numeric($key) ? $attr : $key;
133+
$value = $this->getOrigin($key);
134+
135+
if (!is_null($value)) {
136+
throw new Exception('bind attr has exists:' . $key);
137+
}
138+
139+
$this->setAttr($key, $relation ? $relation->getAttr($attr) : null);
140+
}
141+
142+
return $this;
143+
}
144+
118145
/**
119146
* 关联数据写入
120147
* @access public

library/think/model/relation/OneToOne.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,20 +282,22 @@ protected function match($model, $relation, &$result)
282282
/**
283283
* 绑定关联属性到父模型
284284
* @access protected
285-
* @param Model $model 关联模型对象
286-
* @param Model $result 父模型对象
285+
* @param Model $result 关联模型对象
286+
* @param Model $model 父模型对象
287287
* @return void
288288
* @throws Exception
289289
*/
290290
protected function bindAttr($model, &$result)
291291
{
292292
foreach ($this->bindAttr as $key => $attr) {
293-
$key = is_numeric($key) ? $attr : $key;
294-
if (isset($result->$key)) {
293+
$key = is_numeric($key) ? $attr : $key;
294+
$value = $result->getOrigin($key);
295+
296+
if (!is_null($value)) {
295297
throw new Exception('bind attr has exists:' . $key);
296-
} else {
297-
$result->setAttr($key, $model ? $model->$attr : null);
298298
}
299+
300+
$result->setAttr($key, $model ? $model->getAttr($attr) : null);
299301
}
300302
}
301303

0 commit comments

Comments
 (0)