Skip to content

Commit f4a32fe

Browse files
committed
改进toarray方法的append处理 支持relation.attr方式
改进软删除的关联删除
1 parent c908fcc commit f4a32fe

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/model/concern/Conversion.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,14 @@ public function toArray(): array
148148
// 输出额外属性 必须定义获取器
149149
foreach ($this->getOption('append') as $key => $field) {
150150
if (is_numeric($key)) {
151-
$item[$field] = $this->get($field);
151+
if (strpos($field, '.')) {
152+
[$name, $field] = explode('.', $field, 2);
153+
$relation = $this->getRelationData($name, false);
154+
$value = $relation?->get($field);
155+
} else {
156+
$value = $this->get($field);
157+
}
158+
$item[$field] = $value;
152159
} else {
153160
// 追加关联属性
154161
$relation = $this->getRelationData($key, false);

src/model/concern/SoftDelete.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Closure;
1717
use think\db\BaseQuery as Query;
1818
use think\Model;
19+
use think\model\Collection;
1920

2021
/**
2122
* 数据软删除
@@ -96,15 +97,23 @@ public function delete(): bool
9697
$force = $this->isForce();
9798

9899
if ($name && !$force) {
100+
foreach ($this->getData() as $name => $val) {
101+
if ($val instanceof Model || $val instanceof Collection) {
102+
$relations[$name] = $val;
103+
}
104+
}
99105
// 软删除
100106
$this->exists()->withEvent(false)->save([$name => $this->getDateTime($name)]);
101107

102108
$this->withEvent(true);
103-
109+
if (!empty($relations)) {
110+
// 删除关联数据
111+
$this->relationDelete($relations);
112+
}
104113
$this->trigger('AfterDelete');
105114
$this->exists(false);
106115
$this->clear();
107-
return true;
116+
return true;
108117
}
109118
return parent::delete();
110119
}

0 commit comments

Comments
 (0)