<?php namespace app\index\controller; use think\Controller; use app\index\model\Staff as StaffModel; use think\Db; use think\model\concern\SoftDelete; class Staff extends Controller { public function instance(StaffModel $staff)//依赖注入 { //$staff = new StaffModel();//模型实例化 dump($staff->getName());//查看模型名称 $staff->name = 'peter'; $staff->sex = 0; $staff->age = 11; $staff->salary = 1578; $staff->save(); return '新增成功,id='.$staff->staff_id; } //模型查询 public function query() { //单挑记录:get(主键/闭包) //闭包:匿名回调函数,将函数作为参数进行传递 // new StaffModel(); $staff = StaffModel::get(2); dump($staff); echo '<hr>'; var_dump($staff); echo '<hr>'; echo $staff['name'],'<br>'; echo $staff->name,'<br>'; //用闭包来创建查询条件 $staff = StaffModel::get(function ($query){ $query->where('sex',0)->where('salary','>',8000); }); dump($staff); echo '<hr>'; //直接静态调用Db类的查询构造器进行查询 //StaffModel == Db::table('staff') StaffModel::where('sex',0) ->where('salary','>',8000) ->find(); dump($staff); echo '<hr>'; //多条记录查询:all(主键列表/闭包) //返回值是多个数组、对象数组 //$staff = StaffModel::all(); //$staff = StaffModel::all([1,2,4]); $staffs = StaffModel::all(function ($query){ $query->where('age','<',20)->where('salary','>',1000); }); dump($staffs); echo '<hr>'; foreach ($staffs as $staff){ echo '姓名'.$staff->name.'<br>'; echo '年宁'.$staff->age.'<br>'; echo '工资'.$staff->salary.'<hr>'; } //采用闭包来实现请求变量注入到闭包条件中 //$this->request:请求对象 $age = $this->request->param('age')?:20; $salary = $this->request->param('salary')?:1000; $staffs = StaffModel::all(function ($query) use ($age,$salary){ $query->where('age','<',$age)->where('salary','>',$salary); }); dump($staffs); } //模型更新操作 public function update() { $staff = StaffModel::get(10); $staff->name = '10hao'; $staff->save(); //静态方法:update(数据,条件,字段) // StaffModel::update( //// ['name'=>'十0号'], //// ['staff_id'=>2] //// ); //复杂更新 // StaffModel::update( // ['salary'=>\think\Db::raw('salary-2000')],//更新工资降低2000 // function ($query){ // $query->where('age','>',50);//获取年龄大于50的信息 // } // ); //构造器更新数据 StaffModel::where('age','>',50) ->data(['salary'=>\think\Db::raw('salary-2000')]) ->update(); } //创建模型:添加数据 public function create() { $data = [ 'name'=>'sss888', 'sex'=>'0', 'age'=>252, 'salary'=>8888 ]; //$field = ['name','age'];//限制传入数据类型 //StaffModel::create($data,$field); //用查询构造器来插入数据 StaffModel::insert($data); } //模型删除 public function delete() { //destroy(主键、主键列表、闭包) //StaffModel::destroy(31); //删除条件推荐使用闭包 StaffModel::destroy(function ($query){ $query->where('age','>',55)->where('salary','>=',5000); }); //查询器实现删除 StaffModel::where('age','>',35)->delete(); } //软删除 public function softDelete() { StaffModel::destroy(30); //软删除的数据在普通查询中不可见 // $res = StaffModel::Where('staff_id > 25')->select(); // dump($res); //查看软删除的数据 $res = StaffModel::withTrashed()->Where('staff_id > 25')->select(); //只查看软删除数据 $res = StaffModel::onlyTrashed()->select(); dump($res); } //软删除恢复 public function restore() { // StaffModel::update( // ['delete_time'=>0], // ['staff_id'=>30] // ); StaffModel::onlyTrashed() -> find(30)-> restore();//软查询并使用restorr恢复软删除数据 } }
软删除恢复执行的sql语句
UPDATE `staff` SET `delete_time` = 0 WHERE ( `staff_id` = 30 ) AND `delete_time` <> 0
本质意义是将数据表中的delete_time重置或者更新为0
所以使用更新也可以实现软恢复