模型增加软删除功能

This commit is contained in:
thinkphp
2016-08-05 23:34:21 +08:00
parent bfa36913b9
commit e4280c128e
2 changed files with 59 additions and 26 deletions

View File

@@ -86,7 +86,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 更新时间字段 // 更新时间字段
protected $updateTime = 'update_time'; protected $updateTime = 'update_time';
// 删除时间字段 // 删除时间字段
protected $deleteTime = 'delete_time'; protected static $deleteTime;
// 时间字段取出后的默认时间格式 // 时间字段取出后的默认时间格式
protected $dateFormat = 'Y-m-d H:i:s'; protected $dateFormat = 'Y-m-d H:i:s';
// 字段类型或者格式转换 // 字段类型或者格式转换
@@ -133,6 +133,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$this->autoWriteTimestamp = $this->db()->getConfig('auto_timestamp'); $this->autoWriteTimestamp = $this->db()->getConfig('auto_timestamp');
} }
if (is_null(static::$deleteTime)) {
static::$deleteTime = $this->db()->getConfig('soft_delete_field');
}
// 执行初始化操作 // 执行初始化操作
$this->initialize(); $this->initialize();
} }
@@ -275,7 +279,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
public function setAttr($name, $value, $data = []) public function setAttr($name, $value, $data = [])
{ {
if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime, $this->deleteTime])) { if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime, static::$deleteTime])) {
// 自动写入的时间戳字段 // 自动写入的时间戳字段
if (isset($this->type[$name])) { if (isset($this->type[$name])) {
$type = $this->type[$name]; $type = $this->type[$name];
@@ -777,20 +781,42 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/** /**
* 删除当前的记录 * 删除当前的记录
* @access public * @access public
* @param bool $force 是否强制删除
* @return integer * @return integer
*/ */
public function delete() public function delete($force = false)
{ {
if (false === $this->trigger('before_delete', $this)) { if (false === $this->trigger('before_delete', $this)) {
return false; return false;
} }
if (static::$deleteTime && !$force) {
// 软删除
$this->setAttr(static::$deleteTime, null);
$result = $this->isUpdate()->save();
} else {
$result = $this->db()->delete($this->data); $result = $this->db()->delete($this->data);
}
$this->trigger('after_delete', $this); $this->trigger('after_delete', $this);
return $result; return $result;
} }
/**
* 恢复被软删除的记录
* @access public
* @return integer
*/
public function restore()
{
if (static::$deleteTime) {
// 恢复删除
$this->setAttr(static::$deleteTime, 0);
return $this->isUpdate()->save();
}
return false;
}
/** /**
* 设置自动完成的字段( 规则通过修改器定义) * 设置自动完成的字段( 规则通过修改器定义)
* @access public * @access public
@@ -1000,6 +1026,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$result = $data->with($with)->cache($cache); $result = $data->with($with)->cache($cache);
$data = null; $data = null;
} }
if (static::$deleteTime) {
// 默认不查询软删除数据
$result->where(static::$deleteTime, 0);
}
return $result; return $result;
} }
@@ -1007,9 +1037,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* 删除记录 * 删除记录
* @access public * @access public
* @param mixed $data 主键列表 支持闭包查询条件 * @param mixed $data 主键列表 支持闭包查询条件
* @param bool $force 是否强制删除
* @return integer 成功删除的记录数 * @return integer 成功删除的记录数
*/ */
public static function destroy($data) public static function destroy($data, $force = false)
{ {
$model = new static(); $model = new static();
$query = $model->db(); $query = $model->db();
@@ -1024,7 +1055,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$count = 0; $count = 0;
if ($resultSet) { if ($resultSet) {
foreach ($resultSet as $data) { foreach ($resultSet as $data) {
$result = $data->delete(); $result = $data->delete($force);
$count += $result; $count += $result;
} }
} }

View File

@@ -107,6 +107,8 @@ abstract class Connection
'sql_explain' => false, 'sql_explain' => false,
// Builder类 // Builder类
'builder' => '', 'builder' => '',
// 软删除字段
'soft_delete_field' => '',
]; ];
// PDO连接参数 // PDO连接参数