<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think\model\concern;
use think\db\BaseQuery as Query;
use think\Model;
trait SoftDelete
{
protected $withTrashed = false;
public function trashed(): bool
{
$field = $this->getDeleteTimeField();
if ($field && !empty($this->getOrigin($field))) {
return true;
}
return false;
}
public static function withTrashed(): Query
{
$model = new static();
return $model->withTrashedData(true)->db();
}
protected function withTrashedData(bool $withTrashed)
{
$this->withTrashed = $withTrashed;
return $this;
}
public static function onlyTrashed(): Query
{
$model = new static();
$field = $model->getDeleteTimeField(true);
if ($field) {
return $model
->db()
->useSoftDelete($field, $model->getWithTrashedExp());
}
return $model->db();
}
protected function getWithTrashedExp(): array
{
return is_null($this->defaultSoftDelete) ? ['notnull', ''] : ['<>', $this->defaultSoftDelete];
}
public function delete(): bool
{
if (!$this->isExists() || $this->isEmpty() || false === $this->trigger('BeforeDelete')) {
return false;
}
$name = $this->getDeleteTimeField();
$force = $this->isForce();
if ($name && !$force) {
$this->set($name, $this->autoWriteTimestamp($name));
$result = $this->exists()->withEvent(false)->save();
$this->withEvent(true);
} else {
$where = $this->getWhere();
$result = $this->db()
->where($where)
->removeOption('soft_delete')
->delete();
$this->lazySave(false);
}
if (!empty($this->relationWrite)) {
$this->autoRelationDelete($force);
}
$this->trigger('AfterDelete');
$this->exists(false);
return true;
}
public static function destroy($data, bool $force = false): bool
{
$query = (new static())->withTrashedData(true)->db(false);
if (is_array($data) && key($data) !== 0) {
$query->where($data);
$data = null;
} elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $query]);
$data = null;
} elseif (is_null($data)) {
return false;
}
$resultSet = $query->select($data);
foreach ($resultSet as $result) {
$result->force($force)->delete();
}
return true;
}
public function restore($where = []): bool
{
$name = $this->getDeleteTimeField();
if (!$name || false === $this->trigger('BeforeRestore')) {
return false;
}
if (empty($where)) {
$pk = $this->getPk();
if (is_string($pk)) {
$where[] = [$pk, '=', $this->getData($pk)];
}
}
$this->db(false)
->where($where)
->useSoftDelete($name, $this->getWithTrashedExp())
->update([$name => $this->defaultSoftDelete]);
$this->trigger('AfterRestore');
return true;
}
protected function getDeleteTimeField(bool $read = false)
{
$field = property_exists($this, 'deleteTime') && isset($this->deleteTime) ? $this->deleteTime : 'delete_time';
if (false === $field) {
return false;
}
if (false === strpos($field, '.')) {
$field = '__TABLE__.' . $field;
}
if (!$read && strpos($field, '.')) {
$array = explode('.', $field);
$field = array_pop($array);
}
return $field;
}
protected function withNoTrashed(Query $query): void
{
$field = $this->getDeleteTimeField(true);
if ($field) {
$condition = is_null($this->defaultSoftDelete) ? ['null', ''] : ['=', $this->defaultSoftDelete];
$query->useSoftDelete($field, $condition);
}
}
}