diff --git a/library/think/Model.php b/library/think/Model.php index 12e34bf5..3c90b3b7 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -86,7 +86,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 更新时间字段 protected $updateTime = 'update_time'; // 删除时间字段 - protected $deleteTime = 'delete_time'; + protected static $deleteTime; // 时间字段取出后的默认时间格式 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'); } + if (is_null(static::$deleteTime)) { + static::$deleteTime = $this->db()->getConfig('soft_delete_field'); + } + // 执行初始化操作 $this->initialize(); } @@ -275,7 +279,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ 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])) { $type = $this->type[$name]; @@ -777,20 +781,42 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 删除当前的记录 * @access public + * @param bool $force 是否强制删除 * @return integer */ - public function delete() + public function delete($force = false) { if (false === $this->trigger('before_delete', $this)) { return false; } - $result = $this->db()->delete($this->data); + if (static::$deleteTime && !$force) { + // 软删除 + $this->setAttr(static::$deleteTime, null); + $result = $this->isUpdate()->save(); + } else { + $result = $this->db()->delete($this->data); + } $this->trigger('after_delete', $this); 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 @@ -1000,6 +1026,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $result = $data->with($with)->cache($cache); $data = null; } + if (static::$deleteTime) { + // 默认不查询软删除数据 + $result->where(static::$deleteTime, 0); + } return $result; } @@ -1007,9 +1037,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * 删除记录 * @access public * @param mixed $data 主键列表 支持闭包查询条件 + * @param bool $force 是否强制删除 * @return integer 成功删除的记录数 */ - public static function destroy($data) + public static function destroy($data, $force = false) { $model = new static(); $query = $model->db(); @@ -1024,7 +1055,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $count = 0; if ($resultSet) { foreach ($resultSet as $data) { - $result = $data->delete(); + $result = $data->delete($force); $count += $result; } } diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 6712e0d9..c4e09e6d 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -68,45 +68,47 @@ abstract class Connection // 数据库连接参数配置 protected $config = [ // 数据库类型 - 'type' => '', + 'type' => '', // 服务器地址 - 'hostname' => '', + 'hostname' => '', // 数据库名 - 'database' => '', + 'database' => '', // 用户名 - 'username' => '', + 'username' => '', // 密码 - 'password' => '', + 'password' => '', // 端口 - 'hostport' => '', + 'hostport' => '', // 连接dsn - 'dsn' => '', + 'dsn' => '', // 数据库连接参数 - 'params' => [], + 'params' => [], // 数据库编码默认采用utf8 - 'charset' => 'utf8', + 'charset' => 'utf8', // 数据库表前缀 - 'prefix' => '', + 'prefix' => '', // 数据库调试模式 - 'debug' => false, + 'debug' => false, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) - 'deploy' => 0, + 'deploy' => 0, // 数据库读写是否分离 主从式有效 - 'rw_separate' => false, + 'rw_separate' => false, // 读写分离后 主服务器数量 - 'master_num' => 1, + 'master_num' => 1, // 指定从服务器序号 - 'slave_no' => '', + 'slave_no' => '', // 是否严格检查字段是否存在 - 'fields_strict' => true, + 'fields_strict' => true, // 数据集返回类型 - 'resultset_type' => 'array', + 'resultset_type' => 'array', // 自动写入时间戳字段 - 'auto_timestamp' => false, + 'auto_timestamp' => false, // 是否需要进行SQL性能分析 - 'sql_explain' => false, + 'sql_explain' => false, // Builder类 - 'builder' => '', + 'builder' => '', + // 软删除字段 + 'soft_delete_field' => '', ]; // PDO连接参数