abstract:作业提交:软删除恢复,执行代码如下StaffModel::onlyTrashed()->find(19)->restore();SQL结果:将ID记录为19的软删除了的记录找到,然后update设置的软字段,将值更新为设置的值(0)。//[ SQL ] SELECT * FROM `about` WHERE ( `id` = 19 ) AND `about`.`delete_time`
作业提交:软删除恢复,执行代码如下
StaffModel::onlyTrashed()->find(19)->restore();
SQL结果:将ID记录为19的软删除了的记录找到,然后update设置的软字段,将值更新为设置的值(0)。
//[ SQL ] SELECT * FROM `about` WHERE ( `id` = 19 ) AND `about`.`delete_time` <> '0' LIMIT 1
//[ SQL ] UPDATE `about` SET `delete_time` = 0 WHERE ( `id` = 19 ) AND `delete_time` <> 0
以下内容为学习心得及部分代码
一、创建模型
namespace app\index\model; use think\Model; use think\model\concern\SoftDelete; //trait方法集 class Staff extends Model { use SoftDelete; //设置数据表的名称 protected $table='about'; //设置主键 protected $pk='id'; //设置删除时间的字段名 protected $deleteTime='delete_time'; //设置软删除字段的字段名(可以在模型thinkphp\model.php中查找) protected $defaultSoftDelete=0; }
二、模型的引入及实例化
namespace app\index\controller; use think\Controller; use app\index\model\Staff as StaffModel;//设置模型类的别名,否则跟下面的Staff类同名了 use think\Db; use think\model\concern\SoftDelete; //软删除 class Staff extends Controller { // 各类操作函数...... }
三、模型的各类操作函数
3.1添加数据
//创建模型:添加数据 public function create() { //save() //create(数据, 字段) 是一种静态方法 $data=[ 'orderid'=>3, 'namecn'=>'静态方法添加', 'introctioncn'=>'允许添加的字段,才能添加进去,这个不添加' ]; $filed=['namecn','orderid'];//允许添加的字段 StaffModel::create($data,$filed); //INSERT INTO `about` (`orderid` , `namecn`) VALUES (3 , '静态方法添加') [ RunTime:0.000352s ] //用查询构造器来插入数据 StaffModel::insert($data); //INSERT INTO `about` (`orderid` , `namecn` , `introctioncn`) VALUES (3 , '静态方法添加' , '允许添加的字段,才能添加进去,这个不添加') [ RunTime:0.001747s ] }
3.2 查询
//模型查询 1.单条记录:静态get(主键/闭包)方法 find(),select(), public function query() { $staff=StaffModel::get(33); dump($staff);//数组Array \var_dump($staff);//对象 Object //两种输出方式 echo $staff['namecn'].'<Br>'; //数组方式 echo $staff->namecn; //对象方式 //用闭包来创建查询条件 $staff=StaffModel::get(function($query){//$query参数,在全局已经被自动实例化 $query->where('orderid',3)->where('id','>',10); }); //直接静态调用Db类的查询构造器进行查询 // 因为StaffModel==Db::table('about') 所以用StaffModel静态方法替换第一个链式 StaffModel::where('orderid',3) ->where('id','>',10) ->find(); }
//2 多条记录查询 all(主键列表/闭包) //返回值是多个数组/对象数组 public function queryall() { //$staffs=StaffModel::all(); //SELECT * FROM about //$staffs=StaffModel::all([30,31,32]); //主键列表必须以数组的形式传入。SELECT * FROM `about` WHERE `id` IN (30,31,32) [ RunTime:0.000479s ] //采用闭包来实现,将请求变量注入到闭包条件中 $this->request; //URL请求对象 $this->request=new \think\facade\Request $id=$this->request->param('id') ?: 30; $orderid=$this->request->param('orderid') ?: 3; $staffs=StaffModel::all(function ($query) use($id, $orderid){ //外部变量需要用use引入 $query->where('id','<',$id)->where('orderid','=',$orderid); }); foreach ($staffs as $v){ echo 'id:'.$v->id.'<br>'; echo '标题:'.$v->namecn.'<hr>'; }; }
3.3 更新
//模型更新 public function update() { //基于查询,不允许无条件更新 $staff=StaffModel::get(32); $staff->namecn='update更新';//更新记录字段 $staff->save();//将更新写回表,保存更新值 //静态方法:update(数据, 条件, 字段)//三个参数 StaffModel::update( ['namecn'=>'静态方法更新'], ['id'=>32] ); //复杂更新 //将排序大于3的记录,增加5 StaffModel::update( ['orderid'=> \think\Db::raw('orderid+5')], //['orderid'=>['>',5]]//这种写法是错误的,要用闭包来写 function ($query){ $query->where ('orderid','>',3); } ); //也可以用查询构造器来更新操作 StaffModel::where('orderid','>',5) ->data(['orderid'=> \think\Db::raw('orderid+5')]) ->Update(); //UPDATE `about` SET `orderid` = orderid+5 WHERE `orderid` > 5 [ RunTime:0.001824s ] }
3.4 删除(直接从数据表中删除了记录)
//模型删除:删除记录 public function delete() { //destroy(主键/主键列表/闭包) StaffModel::destroy(17);//主键 //SELECT * FROM `about` WHERE `id` = 17 [ RunTime:0.000505s ] //DELETE FROM `about` WHERE `id` = 17 [ RunTime:0.000376s ] StaffModel::destroy([31,34]);//列表,数组的形式 //[ SQL ] SELECT * FROM `about` WHERE `id` IN (31,34) [ RunTime:0.000442s ] //[ SQL ] DELETE FROM `about` WHERE `id` = 31 [ RunTime:0.000313s ] //[ SQL ] DELETE FROM `about` WHERE `id` = 34 [ RunTime:0.000291s ] //删除条件,推荐使用闭包 StaffModel::destroy(function ($query){ $query->where('id','<',30)->where('orderid','=',10); }); //SELECT * FROM `about` WHERE `id` < 30 AND `orderid` = 10 [ RunTime:0.000451s ] //查询构造器实现删除 StaffModel::where('id','=',21)->delete(); }
3.5 软删除(用更新来模拟删除,需在模型中进行配置)
/*
* 软删除的步骤
* 1、在表中添加一个字段,删除时间(删除标志):delete_time
* 2、在模型类添加一个属性:$deleteTime='delete_time'
* 3、在模型中导入软删除的trait类库:SoftDelete
* 4、最新版支持设置软删除的默认字段值 think\model\concern\;
*
* */
use think\model\concern\SoftDelete; //trait方法集 class Staff extends Model { use SoftDelete; //设置删除时间的字段名 protected $deleteTime='delete_time'; //设置软删除字段的字段名(可以在模型thinkphp\model.php中查找) protected $defaultSoftDelete=0; }
public function softDelete() { StaffModel::destroy([18,19]); //[ SQL ] SELECT * FROM `about` WHERE `id` = 18 [ RunTime:0.000449s ] //[ SQL ] UPDATE `about` SET `delete_time` = 1542386734 WHERE ( `id` = 18 ) AND `about`.`delete_time` = '0' [ RunTime:0.000320s ] //delete_time字段不允许为空,否则更新失败 //软删除记录不会出现在查询结果中 $res = StaffModel::where('id < 30')->select(); //如果想让查询结果包括已经软删除的记录 $res = StaffModel::withTrashed()->where('id<30')->select(); //如果只想查询已经被软删除的数据(回收站) $res = StaffModel::onlyTrashed()->select(); //SELECT * FROM `about` WHERE `about`.`delete_time` <> '0' [ RunTime:0.000390s ] //恢复软删除的内容 restore //$st = StaffModel::onlyTrashed()->find(19); //$st->restore(); StaffModel::onlyTrashed()->find(19)->restore(); //[ SQL ] SELECT * FROM `about` WHERE ( `id` = 19 ) AND `about`.`delete_time` <> '0' LIMIT 1 [ RunTime:0.000468s ] //[ SQL ] UPDATE `about` SET `delete_time` = 0 WHERE ( `id` = 19 ) AND `delete_time` <> 0 [ RunTime:0.001984s ] }
Correcting teacher:韦小宝Correction time:2018-11-17 09:04:04
Teacher's summary:嗯!写的很不错!代码都很完整!不错不错!继续加油吧!