mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 07:32:48 +08:00
改进软删除操作
This commit is contained in:
@@ -280,6 +280,13 @@ abstract class Builder
|
|||||||
|
|
||||||
$whereStr .= empty($whereStr) ? substr(implode(' ', $str), strlen($key) + 1) : implode(' ', $str);
|
$whereStr .= empty($whereStr) ? substr(implode(' ', $str), strlen($key) + 1) : implode(' ', $str);
|
||||||
}
|
}
|
||||||
|
if (!empty($options['soft_delete'])) {
|
||||||
|
// 附加软删除条件
|
||||||
|
list($field, $condition) = $options['soft_delete'];
|
||||||
|
|
||||||
|
$whereStr = $whereStr ? '( ' . $whereStr . ' ) AND ' : '';
|
||||||
|
$whereStr = $whereStr . $this->parseWhereItem($field, $condition, '', $options, $binds);
|
||||||
|
}
|
||||||
return $whereStr;
|
return $whereStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1120,6 +1120,20 @@ class Query
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置软删除字段及条件
|
||||||
|
* @access public
|
||||||
|
* @param false|string $field 查询字段
|
||||||
|
* @param mixed $condition 查询条件
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function useSoftDelete($field, $condition = null)
|
||||||
|
{
|
||||||
|
if ($field) {
|
||||||
|
$this->options['soft_delete'] = [$field, $condition ?: ['null', '']];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分析查询表达式
|
* 分析查询表达式
|
||||||
* @access public
|
* @access public
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ trait SoftDelete
|
|||||||
{
|
{
|
||||||
$model = new static();
|
$model = new static();
|
||||||
$field = $model->getDeleteTimeField(true);
|
$field = $model->getDeleteTimeField(true);
|
||||||
return $model->db(false)->removeWhereField($field);
|
return $model->db(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -42,7 +42,7 @@ trait SoftDelete
|
|||||||
{
|
{
|
||||||
$model = new static();
|
$model = new static();
|
||||||
$field = $model->getDeleteTimeField(true);
|
$field = $model->getDeleteTimeField(true);
|
||||||
return $model->db(false)->whereNotNull($field);
|
return $model->db(false)->useSoftDelete($field, ['not null', '']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,10 +114,9 @@ trait SoftDelete
|
|||||||
if (empty($where)) {
|
if (empty($where)) {
|
||||||
$pk = $this->getPk();
|
$pk = $this->getPk();
|
||||||
$where[$pk] = $this->getData($pk);
|
$where[$pk] = $this->getData($pk);
|
||||||
$where[$name] = ['not null', ''];
|
|
||||||
}
|
}
|
||||||
// 恢复删除
|
// 恢复删除
|
||||||
return $this->db(false)->removeWhereField($this->getDeleteTimeField(true))->where($where)->update([$name => null]);
|
return $this->db(false)->useSoftDelete($name, ['not null', ''])->where($where)->update([$name => null]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -129,7 +128,7 @@ trait SoftDelete
|
|||||||
protected function base($query)
|
protected function base($query)
|
||||||
{
|
{
|
||||||
$field = $this->getDeleteTimeField(true);
|
$field = $this->getDeleteTimeField(true);
|
||||||
$query->whereNull($field);
|
$query->useSoftDelete($field);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user