模型各项基本操作

Original 2018-12-22 23:32:47 253
abstract:controller<?php  namespace app\index\controller; use think\Controller; use app\index\model\Staff as StaffModel;//设置模型类别名 class Staff extends Controlle

controller

<?php 
namespace app\index\controller;

use think\Controller;
use app\index\model\Staff as StaffModel;//设置模型类别名

class Staff extends Controller
{
    //实例化模型
    // public function instance()
    public function instance(StaffModel $staff)//依赖注入
    {
        // $staff = new StaffModel();//模型实例化
        dump($staff->getName());//查看模型名称
        //新增一条记录
        $staff->name = 'Eddie';
        $staff->sex = 0;
        $staff->age = 32;
        $staff->salary = 3500;

        //新增操作
        $staff->save();

        return '新增成功,id='.$staff->staff_id;
    }
    //模型查询
    public function query()
    {
        // find(),select()
        //单条记录:get(主键/闭包)
        //闭包就是一个匿名回调函数,将函数作为参数进行传递
        // new StaffModel();
        $staff = StaffModel::get(2);
        dump($staff);//Array
        echo '<hr>';
        \var_dump($staff);//Object
        echo '<hr>';
        echo $staff['name'],'<br>';
        echo $staff->name,'<hr>';

        //用闭包创建查询条件
        // $staff  = StaffModel::get(function($query){
        //     $query->where('sex',0)->where('salary','>',8000);
        // });

        // echo '性别为男,工资大于8000的员工信息<br>';
        // dump($staff);
        echo '<hr>';

        //直接静态调用Db类的查询构造器进行查询
        // StaffModel ==Db::table('staff')
        $staff = StaffModel::where('sex',0)
        ->where('salary','>',6000)
        ->find();
        dump($staff);
        echo '<hr>';
        echo '<hr>';
        echo '<hr>';

        //2 多条记录查询:all(主键列表/闭包)
        //返回值是多个数组/对象数组
        // $staffs = StaffModel::all();
        // $staffs = StaffModel::all([1,2,3]);
        // $staffs = StaffModel::all(function($query){
        //     $query->where('age','<=',40)->where('salary','>',5000);
        // });
        // dump($staffs);
        // foreach ($staffs as $staff ) {
        //     # code...
        //     echo '姓名:'.$staff->name.'<br>';
        //     echo '性别:'.$staff->sex.'<br>';
        //     echo '年龄:'.$staff->age.'<br>';
        //     echo '工资:'.$staff->salary.'<hr color="red">';
        // }

        //采用闭包实现将请求变量注入到闭包条件中
        // $this->request:请求对象
        $age = $this->request->param('age')?:40;
        $salary = $this->request->param('salary')?:3000;

        $staffs = StaffModel::all(function($query) use ($age,$salary){
            $query->where('age','<=',$age)->where('salary','>',$salary);
        });

        dump($staffs);

    }

    //模型更新
    public function update()
    {
        //基于查询,不允许无条件更新
        
        // $staff = StaffModel::get(2);
        // $staff->name = '姑姑';//更新
        // $staff->save();//回写
        
        // 静态方法:update(数据,条件,字段)
        StaffModel::update(
            ['name'=>'小龙女'],
            ['staff_id'=>2]
        );

        //复杂更新
        //年龄大于40,加工资100
        StaffModel::update(
            ['salary'=>\think\Db::raw('salary+100')],
            function($query){
                $query->where('age','>=',40);
            }
        );

        // 查询构造器更新数据
        StaffModel::where('age','<',25)
            ->data(['salary'=>\think\Db::raw('salary-100')])
            ->update();
    }

    // 创建模型:添加数据
    public function create()
    {
        // creat(数据,字段)
        $data = [
            'name'=>'孙悟空2',
            'sex'=>0,
            'age'=>99,
            'salary'=>8888
        ];
        $data2 = [
            'name'=>'猪悟能',
            'sex'=>0,
            'age'=>23,
            'salary'=>5600
        ];

        // $field = ['name','sex','age','salary'];
        $field = ['name','age'];
        // StaffModel::create($data,$field);
        // 
        // 用查询构造器插入数据
        StaffModel::insert($data2);

    }

    //软删除操作,先在模型中进行配置
    public function softdelete()
    {
        StaffModel::destroy([1,2]);
        //UPDATE `staff` SET `delete_time` = 1545489290 WHERE ( `staff_id` = 1 ) AND `staff`.`delete_time` = '0'
        //
        //软删除的数据在普通查询中不可见
        $res = StaffModel::where('staff_id < 5')->select();

        //想看到已经软删除的数据
        $res = StaffModel::withTrashed()->where('staff_id < 5')->select();
        
        //只想看回收站:
        $res = StaffModel::onlyTrashed()->select();
        

        dump($res);
    }

    //恢复数据
    public function undelete()
    {
        if(StaffModel::onlyTrashed()->find())
            {
                StaffModel::onlyTrashed()->find()->restore();
            }

    }


}

model

<?php

namespace app\index\model;

use think\Model;

use think\model\concern\SoftDelete;//trait方法集

class Staff extends Model
{

    use SoftDelete;
    //设置数据表的名称
    protected $table = 'staff';
    //设置主键:默认为id
    protected $pk = 'staff_id';

    //设置软删除时间字段名
    protected $deleteTime = 'delete_time';

    //设置软删除字段默认值
    protected $defaultSoftDelete = 0;
}


Correcting teacher:天蓬老师Correction time:2018-12-23 10:06:57
Teacher's summary:作业完成的不错,最好能加上运行的效果图,还有自己对模型操作的理解,比如优点和缺点

Release Notes

Popular Entries