abstract:<?phpnamespace app\admin\model;use think\Model;use think\model\concern\SoftDelete;/* * 軟刪除步驟: * 1.在表中添加一個(gè)字段: 刪除時(shí)間(刪除標(biāo)志): delete_time * 2.在模型類(lèi)添加一個(gè)屬性: $deleteTime = 'delete_time' * 3.在模型中導(dǎo)入軟刪
<?php
namespace app\admin\model;
use think\Model;
use think\model\concern\SoftDelete;
/*
* 軟刪除步驟:
* 1.在表中添加一個(gè)字段: 刪除時(shí)間(刪除標(biāo)志): delete_time
* 2.在模型類(lèi)添加一個(gè)屬性: $deleteTime = 'delete_time'
* 3.在模型中導(dǎo)入軟刪除的trait類(lèi)庫(kù): SoftDelete
* 4.最新版本支持設(shè)置軟刪除的默認(rèn)字段值
* */
class Staff extends Model
{
use SoftDelete;
//設(shè)置數(shù)據(jù)表的名稱(chēng) = 'staff';
protected $table = 'staff';
//設(shè)置主鍵: 默認(rèn)為id
protected $pk = 'staff_id';
//設(shè)置軟刪除時(shí)間的字段名
protected $defaultSoftDelete = 0;
//軟刪除:必須在模型中進(jìn)行配置
public function softDelete(){
$res = StaffModel::withTrashed()->where('staff_id < 5')->select();
dump($res);
}
}
DROP TABLE IF EXISTS `staff`;
CREATE TABLE `staff` (
`staff_id` int(4) NOT NULL COMMENT '主鍵id',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`sex` int(1) DEFAULT NULL COMMENT '性別0男1女',
`age` tinyint(4) DEFAULT NULL COMMENT '年齡',
`salary` int(6) DEFAULT NULL COMMENT '工資',
`delete_time` int(10) DEFAULT NULL,
PRIMARY KEY (`staff_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Correcting teacher:查無(wú)此人Correction time:2019-07-19 13:11:30
Teacher's summary:完成的不錯(cuò)。線(xiàn)上項(xiàng)目的mysql,不允許刪除數(shù)據(jù)。所以才出現(xiàn)軟刪除。繼續(xù)加油