内核更新
bug修复
This commit is contained in:
@@ -12,7 +12,8 @@ trait SoftDelete
|
||||
*/
|
||||
public function trashed()
|
||||
{
|
||||
if (!empty($this->data[static::$deleteTime])) {
|
||||
$field = $this->getDeleteTimeField();
|
||||
if (!empty($this->data[$field])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -37,7 +38,8 @@ trait SoftDelete
|
||||
public static function onlyTrashed()
|
||||
{
|
||||
$model = new static();
|
||||
return $model->db()->where(static::$deleteTime, 'exp', 'is not null');
|
||||
$field = $model->getDeleteTimeField();
|
||||
return $model->db()->where($field, 'exp', 'is not null');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,10 +53,9 @@ trait SoftDelete
|
||||
if (false === $this->trigger('before_delete', $this)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (static::$deleteTime && !$force) {
|
||||
$name = $this->getDeleteTimeField();
|
||||
if (!$force) {
|
||||
// 软删除
|
||||
$name = static::$deleteTime;
|
||||
$this->change[] = $name;
|
||||
$this->data[$name] = $this->autoWriteTimestamp($name);
|
||||
$result = $this->isUpdate()->save();
|
||||
@@ -106,12 +107,10 @@ trait SoftDelete
|
||||
*/
|
||||
public function restore($where = [])
|
||||
{
|
||||
if (static::$deleteTime) {
|
||||
// 恢复删除
|
||||
$name = static::$deleteTime;
|
||||
return $this->isUpdate()->save([$name => null], $where);
|
||||
}
|
||||
return false;
|
||||
$name = $this->getDeleteTimeField();
|
||||
// 恢复删除
|
||||
return $this->isUpdate()->save([$name => null], $where);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,11 +119,28 @@ trait SoftDelete
|
||||
* @param \think\db\Query $query 查询对象
|
||||
* @return void
|
||||
*/
|
||||
protected static function base($query)
|
||||
protected function base($query)
|
||||
{
|
||||
if (static::$deleteTime) {
|
||||
$query->where(static::$deleteTime, 'null');
|
||||
}
|
||||
$field = $this->getDeleteTimeField(true);
|
||||
$query->where($field, 'null');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取软删除字段
|
||||
* @access public
|
||||
* @param bool $read 是否查询操作 写操作的时候会自动去掉表别名
|
||||
* @return string
|
||||
*/
|
||||
protected function getDeleteTimeField($read = false)
|
||||
{
|
||||
if (isset($this->deleteTime)) {
|
||||
$field = $this->deleteTime;
|
||||
} else {
|
||||
$field = 'delete_time';
|
||||
}
|
||||
if (!$read && strpos($field, '.')) {
|
||||
list($alias, $field) = explode('.', $field);
|
||||
}
|
||||
return $field;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user