Skip to content

Commit

Permalink
增加clear方法 清空模型数据
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Dec 24, 2024
1 parent 80cafc4 commit 4248b05
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,8 @@ public function save(array | object $data = [], $where = []): bool
$this->relationSave($relations);
}

// 重置原始数据
$this->setWeakMap('origin', $data);
return true;
}

Expand All @@ -798,7 +800,7 @@ protected function autoDateTime(array &$data, bool $update)
foreach ($dateTimeFields as $field) {
if (is_string($field)) {
$data[$field] = $this->getDateTime($field);
$this->$field = $this->readTransform($data[$field], $this->getFields($field));
$this->setData($field, $this->readTransform($data[$field], $this->getFields($field)));
}
}
}
Expand Down Expand Up @@ -926,6 +928,7 @@ public function isSimple(): bool
public function delete(): bool
{
if ($this->isVirtual() || $this->isView()) {
$this->clear();
return true;
}

Expand All @@ -948,6 +951,7 @@ public function delete(): bool
$this->relationDelete($relations);
}

$this->clear();
return true;
}

Expand Down Expand Up @@ -1099,6 +1103,20 @@ public function data(array $data)
return $this;
}

/**
* 清空模型数据.
*
* @return $this
*/
public function clear()
{
self::$weakMap[$this]['data'] = [];
self::$weakMap[$this]['origin'] = [];
self::$weakMap[$this]['get'] = [];
self::$weakMap[$this]['relation'] = [];
return $this;
}

/**
* 获取原始数据.
*
Expand Down

0 comments on commit 4248b05

Please sign in to comment.