Correction status:qualified
Teacher's comments:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/25 0025 * Time: 下午 1:53 */ namespace app\index\controller; use think\Container; use app\index\model\EmpModel; use think\Request; class Emp extends Container{ // public function instance(EmpModel $empModel){//依赖注入 那是 方法里边的EmpModel 自动实例化了。。 instance 方法名报错 public function getData(EmpModel $empModel){//依赖注入 那是 方法里边的EmpModel 自动实例化了。。 这个正常 // $empModel = new EmpModel();//可以内部直接实例化 dump($empModel->getName()); //新增一条记录 $empModel->name='QB'; $empModel->grade=1; $empModel->email='admin@qq.com'; $empModel->salary='1000 '; $empModel->save(); // return "新增成功= ".$empModel->getLastInsID(); return "新增成功= ".$empModel->id; } //模型查询 public function query(){ //1单条记录 get(主键/闭包) //闭包就是一个匿名回调函数 /* $emp = EmpModel::get(38); dump($emp);//dump返回的是经过处理的数组 \var_dump($emp);//默认数据源返回的是个对象 echo $emp->name; echo "<hr>";*/ /* $emp_1 = EmpModel::get(function ($query){ $query->where('id',100); }); dump($emp_1);*/ /*$emp_2 = EmpModel::where('grade',1) ->where("salary",">","500") ->select(); dump($emp_2,true,"find");*/ /* $emp_3=EmpModel::all([100,250,5247,1475,2658,41447,1475]); dump($emp_3,true,"all"); foreach ($emp_3 as $item) {//貌似tp内部处理了 这里用数组获取字段 和 用对象获取字段效果一样 echo "姓名".$item['name']."<br>"; echo "等级".$item->grade."<br>"; echo "邮箱".$item->email."<br>"; echo "工资".$item->salary."<hr>"; }*/ //采用闭包来实现将请求变量注入到闭包条件中 //$this->request:请求对象 // $request = new Request(); $grade = $this->request->param('grade')?:2; $salary = $this->request->param('salary')?:500; // $emp_4 = EmpModel::all(function ($query)/* use ($grade,$salary)*/{ // $query->where('grade','<',/*$grade*/2)->where('salary','>',/*$salary*/500); // }); // dump($emp_4); } public function getAll(Request $request){ // $grade = $this->request->param('grade')?:2; // $salary = $this->request->param('salary')?:500; // $grade = $request->param('grade') ? : 1;//门脸用不了了 // $salary = $request->param('salary')?:500; $grade = $request->param('grade') ? : 1;//门脸用不了了 $salary = $request->param('salary')?:500; $emp_4 = EmpModel::all(function ($query) use ($grade,$salary){ $query->where('grade','>',$grade)->where('salary','>',$salary); }); dump($emp_4); } public function update(Request $request){//更新数据 $id = $request->param('id'); $name = $request->param('name'); $salary = $request->param('salary'); //方法 一: // $empUpdate = EmpModel::where('id',$id) // ->find(); // dump($empUpdate); // $empUpdate->name=$name; // $empUpdate->save();//这种方式不太推荐使用 可以直接使用 update静态方法 // dump($empUpdate); //方法 二:直接调用update方法 // $data=[ // 'name'=>$name // ]; // $where=[ // 'id'=>$id // ]; // $emp_update_result = EmpModel::update($data,$where); // dump($emp_update_result); //方法三;使用闭包更新 $data = [ 'salary'=> \think\Db::raw('salary+500') ]; $emp_add_salary = EmpModel::update($data,function ($query) use ($salary){ $query->where('salary','>',$salary); }); dump($emp_add_salary); } public function AddData(Request $request){//新增数据,可以用静态方法create 或者 用 save方法 上边有演示 $name = $request->param('name'); $grade = $request->param('grade'); $email = $request->param('email'); $salary = $request->param('salary'); $data = [ 'name'=>$name, 'grade'=>$grade, 'email'=>$email, 'salary'=>$salary ]; $field=[//默认允许新增的字段 'name', 'grade', // 'email', 'salary' ]; $emp_add_result = EmpModel::create($data,$field); dump($emp_add_result); } public function deleteData(Request $request){ // start // EmpModel::destroy(74812);//根据主键删除数据 内部主键可以是个数组 /** * 执行顺序 * [ DB ] CONNECT:[ UseTime:0.003000s ] mysql:host=127.0.0.1;dbname=work;charset=utf8 * [ SQL ] SHOW COLUMNS FROM `emp` [ RunTime:0.027002s ] * [ SQL ] SELECT * FROM `emp` WHERE `id` = 74812 [ RunTime:0.000000s ] * [ SQL ] DELETE FROM `emp` WHERE `id` = 74812 [ RunTime:0.001000s ] */ // end // start $id = $request->param('id'); //删除推荐使用闭包删除 EmpModel::destroy(function ($query) use ($id){//这里跟更新一样可以接受参数 $query->where('id',$id); }); /** * [ DB ] CONNECT:[ UseTime:0.001000s ] mysql:host=127.0.0.1;dbname=work;charset=utf8 * [ SQL ] SHOW COLUMNS FROM `emp` [ RunTime:0.029002s ] * [ SQL ] SELECT * FROM `emp` WHERE `id` = 74813 [ RunTime:0.000000s ] * [ SQL ] DELETE FROM `emp` WHERE `id` = 74813 [ RunTime:0.000000s ] */ // end // start // EmpModel::where('id',$id)->delete();//使用查询构造器删除数据 /** * [ DB ] CONNECT:[ UseTime:0.010001s ] mysql:host=127.0.0.1;dbname=work;charset=utf8 * [ SQL ] SHOW COLUMNS FROM `emp` [ RunTime:0.032001s ] * [ SQL ] DELETE FROM `emp` WHERE `id` = 74810 [ RunTime:0.000000s ] */ // end } public function softDelete(Request $request){ $id = $request->param('id'); EmpModel::destroy($id); /** * [ DB ] CONNECT:[ UseTime:0.003000s ] mysql:host=127.0.0.1;dbname=work;charset=utf8 * [ SQL ] SHOW COLUMNS FROM `emp` [ RunTime:0.031002s ] * [ SQL ] SELECT * FROM `emp` WHERE `id` = 74819 [ RunTime:0.001000s ] * [ SQL ] UPDATE `emp` SET `delete_time` = 1527324100 WHERE `id` = 74819 [ RunTime:0.000000s ] */ } public function getBigId(){ $res = EmpModel::where('id','>','74794')->select(); dump($res); $resSoftDelete =EmpModel::withTrashed()->where('id','>','74794')->select(); dump($resSoftDelete); } public function restoreData(){ $where = [ 'id'=>74819 ]; // $emp = EmpModel::onlyTrashed()->find(); // $emp->restore(); $emp = new EmpModel(); $emp->restore($where); } }
点击 "运行实例" 按钮查看在线实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/25 0025 * Time: 下午 12:03 */ namespace app\index\model; use think\Model; use think\model\concern\SoftDelete; class EmpModel extends Model{ use SoftDelete;//(⊙o⊙)…这个东西实在模型中引入的。 //设置数据表名 protected $table = 'emp'; //设置主键 主键默认是id 如果 数据表中不是的话 需要特殊指定 protected $pk = 'id'; //设置删除时间字段名 protected $deleteTime = 'delete_time'; protected $defaultSoftDelete=0; }
点击 "运行实例" 按钮查看在线实例