Skip to content

Commit 297640a

Browse files
committed
增加setAttr和getAttr兼容方法
1 parent 7490f09 commit 297640a

File tree

8 files changed

+40
-64
lines changed

8 files changed

+40
-64
lines changed

src/db/concern/ModelRelationQuery.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace think\db\concern;
1515

1616
use Closure;
17-
use think\Entity;
1817
use think\helper\Str;
1918
use think\model\Collection as ModelCollection;
2019
use think\model\contract\Modelable as Model;

src/model/Pivot.php

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

1414
namespace think\model;
1515

16-
use think\Entity;
1716
use think\Model;
1817

1918
/**
@@ -46,8 +45,8 @@ public function __construct(array $data = [], ?Model $parent = null, string $tab
4645
{
4746
$this->parent = $parent;
4847

49-
if (is_null($this->name)) {
50-
$this->name = $table;
48+
if (is_null($this->getOption('name')) {
49+
$this->setOption('name', $table);
5150
}
5251

5352
parent::__construct($data);
@@ -57,22 +56,15 @@ public function __construct(array $data = [], ?Model $parent = null, string $tab
5756
* 创建新的模型实例.
5857
*
5958
* @param array $data 数据
60-
* @param mixed $where 更新条件
61-
* @param array $options 参数
6259
*
6360
* @return Model
6461
*/
65-
public function newInstance(array $data = [], $where = null, array $options = []): Model
62+
public function newInstance(array $data = [])
6663
{
67-
$model = parent::newInstance($data, $where, $options);
64+
$model = parent::newInstance($data);
6865

69-
if ($model instanceof Entity) {
70-
$model->setParent($this->parent);
71-
$model->setOption('table_name', $this->name);
72-
} else {
73-
$model->parent = $this->parent;
74-
$model->name = $this->name;
75-
}
66+
$model->parent = $this->parent;
67+
$model->setOption('name', $this->getOption('name'));
7668

7769
return $model;
7870
}

src/model/concern/Attribute.php

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use think\model\contract\EnumTransform;
2424
use think\model\contract\FieldTypeTransform;
2525
use think\model\contract\Typeable;
26-
use think\model\contract\Modelable;
26+
use think\model\contract\Modelable as Model;
2727
use think\model\type\Date;
2828
use think\model\type\DateTime;
2929
use think\model\type\Json;
@@ -437,19 +437,13 @@ public function isJsonAssoc(): bool
437437
*/
438438
public function set(string $name, $value)
439439
{
440-
$ignore = $this->getOption('ignore', []);
441440
$name = $this->getMappingName($name);
442441
$type = $this->getFields($name);
443442

444-
if (in_array($name, $ignore)) {
445-
// 忽略属性
446-
return $this;
447-
}
448-
449-
if (is_null($value) && is_subclass_of($type, Entity::class)) {
443+
if (is_null($value) && is_subclass_of($type, Model::class)) {
450444
// 关联数据为空 设置一个空模型
451445
$value = new $type();
452-
} elseif (!($value instanceof Modelable || $value instanceof Collection)) {
446+
} elseif (!($value instanceof Model || $value instanceof Collection)) {
453447
// 类型自动转换
454448
$value = $this->readTransform($value, $type);
455449
}
@@ -508,13 +502,7 @@ private function setWithAttr(string $name, $value, array $data = [])
508502
*/
509503
public function get(string $name, bool $attr = true)
510504
{
511-
$ignore = $this->getOption('ignore', []);
512505
$name = $this->getMappingName($name);
513-
if (in_array($name, $ignore)) {
514-
// 忽略属性
515-
return null;
516-
}
517-
518506
if ($attr && $value = $this->getWeakData('get', $name)) {
519507
// 已经输出的数据直接返回
520508
return $value;
@@ -580,16 +568,27 @@ private function getWithAttr(string $name, $value, array $data = [])
580568
}
581569

582570
/**
583-
* 设置忽略属性.
571+
* 使用获取器获取数据对象的值
584572
*
585-
* @param array $ignore 忽略属性列表
573+
* @param string $name 名称
586574
*
587-
* @return $this
575+
* @return mixed
588576
*/
589-
public function ignore(array $ignore)
577+
public function getAttr(string $name)
590578
{
591-
$this->setOption('ignore', $ignore);
579+
return $this->get($name);
580+
}
592581

593-
return $this;
594-
}
582+
/**
583+
* 设置数据对象的值 并进行类型自动转换
584+
*
585+
* @param string $name 名称
586+
* @param mixed $value 值
587+
*
588+
* @return $this
589+
*/
590+
public function setAttr(string $name, $value)
591+
{
592+
return $this->set($name, $value);
593+
}
595594
}

src/model/concern/RelationShip.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private function parseRelationData(array $relations)
6868
if (!empty($bind)) {
6969
// 绑定关联属性
7070
$this->bindRelationAttr($val, $bind);
71-
} elseif (is_subclass_of($type, Entity::class)) {
71+
} elseif (is_subclass_of($type, Model::class)) {
7272
// 明确类型直接设置关联属性
7373
$this->$relation = new $type($val);
7474
} else {
@@ -134,18 +134,6 @@ private function relationDelete(array $relations = [])
134134
}
135135
}
136136

137-
/**
138-
* 获取关联的外键名.
139-
*
140-
* @param string $relation 关联名
141-
* @return string|null
142-
*/
143-
protected function getRelationKey(string $relation)
144-
{
145-
$relationKey = $this->getOption('relation_keys', []);
146-
return $relationKey[$relation] ?? null;
147-
}
148-
149137
/**
150138
* 获取关联数据
151139
*
@@ -168,26 +156,28 @@ protected function getRelationData(string $name)
168156

169157
protected function getBindAttr($bind, $name)
170158
{
171-
if (true === $bind || (isset($bind[$name]) && true === $bind[$name])) {
172-
return true;
173-
}
174159
return $bind[$name] ?? [];
175160
}
176161

177162
/**
178163
* 设置关联绑定数据
179164
*
180-
* @param Entity|array $entity 关联实体对象
181-
* @param array|bool $bind 绑定属性
165+
* @param Model|array $model 关联对象
166+
* @param array $bind 绑定属性
182167
* @return void
183168
*/
184-
public function bindRelationAttr($entity, $bind = [])
169+
public function bindRelationAttr(Model | array $model, array $bind = [])
185170
{
186-
$data = is_array($entity) ? $entity : $entity->getData();
171+
$data = is_array($model) ? $model : $model->getData();
187172
foreach ($data as $key => $val) {
188173
if (isset($bind[$key])) {
189-
$this->set($bind[$key], $val);
190-
} elseif ((true === $bind || in_array($key, $bind)) && !$this->__isset($key)) {
174+
$this->set($bind[$key], $val);
175+
} elseif ($attr = array_search($key, $bind)) {
176+
if (is_numeric($attr) || $this->__isset($attr)) {
177+
} else {
178+
$this->set($attr, $val);
179+
}
180+
} elseif (in_array($key, $bind) && !$this->__isset($key)) {
191181
$this->set($key, $val);
192182
}
193183
}

src/model/relation/BelongsTo.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use Closure;
1717
use think\db\BaseQuery as Query;
18-
use think\Entity;
1918
use think\model\contract\Modelable as Model;
2019

2120
/**

src/model/relation/HasOne.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use Closure;
1717
use think\db\BaseQuery as Query;
18-
use think\Entity;
1918
use think\model\contract\Modelable as Model;
2019

2120
/**

src/model/relation/OneToOne.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use think\db\BaseQuery as Query;
1717
use think\db\exception\DbException as Exception;
1818
use think\db\exception\InvalidArgumentException;
19-
use think\Entity;
2019
use think\helper\Str;
2120
use think\model\contract\Modelable as Model;
2221
use think\model\Relation;

tests/stubs/UserModel.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public function profile(): HasOne
2424
return $this->hasOne(ProfileModel::class, 'uid')
2525
->bind([
2626
'email',
27-
'new_name' => 'nickname',
28-
'call_name' => fn ($model) =>$model?->get('nickname')
27+
'new_name' => 'nickname'
2928
]);
3029
}
3130
}

0 commit comments

Comments
 (0)