|
23 | 23 | use think\model\contract\EnumTransform; |
24 | 24 | use think\model\contract\FieldTypeTransform; |
25 | 25 | use think\model\contract\Typeable; |
26 | | -use think\model\contract\Modelable; |
| 26 | +use think\model\contract\Modelable as Model; |
27 | 27 | use think\model\type\Date; |
28 | 28 | use think\model\type\DateTime; |
29 | 29 | use think\model\type\Json; |
@@ -437,19 +437,13 @@ public function isJsonAssoc(): bool |
437 | 437 | */ |
438 | 438 | public function set(string $name, $value) |
439 | 439 | { |
440 | | - $ignore = $this->getOption('ignore', []); |
441 | 440 | $name = $this->getMappingName($name); |
442 | 441 | $type = $this->getFields($name); |
443 | 442 |
|
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)) { |
450 | 444 | // 关联数据为空 设置一个空模型 |
451 | 445 | $value = new $type(); |
452 | | - } elseif (!($value instanceof Modelable || $value instanceof Collection)) { |
| 446 | + } elseif (!($value instanceof Model || $value instanceof Collection)) { |
453 | 447 | // 类型自动转换 |
454 | 448 | $value = $this->readTransform($value, $type); |
455 | 449 | } |
@@ -508,13 +502,7 @@ private function setWithAttr(string $name, $value, array $data = []) |
508 | 502 | */ |
509 | 503 | public function get(string $name, bool $attr = true) |
510 | 504 | { |
511 | | - $ignore = $this->getOption('ignore', []); |
512 | 505 | $name = $this->getMappingName($name); |
513 | | - if (in_array($name, $ignore)) { |
514 | | - // 忽略属性 |
515 | | - return null; |
516 | | - } |
517 | | - |
518 | 506 | if ($attr && $value = $this->getWeakData('get', $name)) { |
519 | 507 | // 已经输出的数据直接返回 |
520 | 508 | return $value; |
@@ -580,16 +568,27 @@ private function getWithAttr(string $name, $value, array $data = []) |
580 | 568 | } |
581 | 569 |
|
582 | 570 | /** |
583 | | - * 设置忽略属性. |
| 571 | + * 使用获取器获取数据对象的值 |
584 | 572 | * |
585 | | - * @param array $ignore 忽略属性列表 |
| 573 | + * @param string $name 名称 |
586 | 574 | * |
587 | | - * @return $this |
| 575 | + * @return mixed |
588 | 576 | */ |
589 | | - public function ignore(array $ignore) |
| 577 | + public function getAttr(string $name) |
590 | 578 | { |
591 | | - $this->setOption('ignore', $ignore); |
| 579 | + return $this->get($name); |
| 580 | + } |
592 | 581 |
|
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 | + } |
595 | 594 | } |
0 commit comments