Skip to content

Commit 04b9206

Browse files
committed
修正软删除方法兼容
1 parent e91aba1 commit 04b9206

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/Model.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,10 @@ public static function update(array | object $data, $where = [], array $allowFie
646646
*/
647647
public static function destroy($data, bool $force = false, array $together = []): bool
648648
{
649-
$model = new static();
650-
$db = $model->db();
649+
if (empty($data) && 0 !== $data) {
650+
return false;
651+
}
652+
$db = (new static())->db();
651653

652654
if (is_array($data) && key($data) !== 0) {
653655
$db->where($data);

src/model/concern/SoftDelete.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ public function delete(): bool
114114
*
115115
* @param mixed $data 主键列表 支持闭包查询条件
116116
* @param bool $force 是否强制删除
117+
* @param array $together 关联删除
117118
*
118119
* @return bool
119120
*/
120-
public static function destroy($data, bool $force = false): bool
121+
public static function destroy($data, bool $force = false, array $together = []): bool
121122
{
122123
// 传入空值(包括空字符串和空数组)的时候不会做任何的数据删除操作,但传入0则是有效的
123124
if (empty($data) && 0 !== $data) {
@@ -133,15 +134,15 @@ public static function destroy($data, bool $force = false): bool
133134
$query->where($data);
134135
$data = [];
135136
} elseif ($data instanceof Closure) {
136-
call_user_func_array($data, [ &$query]);
137+
$data($query);
137138
$data = [];
138139
}
139140

140141
$resultSet = $query->select((array) $data);
141142

142143
foreach ($resultSet as $result) {
143144
/** @var Model $result */
144-
$result->force($force)->delete();
145+
$result->force($force)->together($together)->delete();
145146
}
146147

147148
return true;

0 commit comments

Comments
 (0)