在实际开发中,采用模型来操作数据库是必备基础知识

Original 2019-04-15 18:53:22 270
abstract:<?php namespace app\index\controller; use think\Db; class Query {     //读操作返回的都是二维数组,没有满足条件的记录返回的是一个空数组     //写操作返回的是受影响的记录数,没有返回0  &nbs
<?php
namespace app\index\controller;
use think\Db;

class Query
{
    //读操作返回的都是二维数组,没有满足条件的记录返回的是一个空数组
    //写操作返回的是受影响的记录数,没有返回0
    public function find()
    {
        //查询一条数据:table里是表名,find是主键ID=10
//        $res = Db::table('staff')->find(10);
        $res = Db::table('sp')
//            ->field('name,jiage')
            ->field(['name as 姓名','jiage as 价格'])         //写在数组中可以写别名
            ->where('jiage','>',11)  //where(字段,表达式,条件)
            ->find();
        //执行后的SQL语句: SELECT * FROM `staff` WHERE `staff_id` > 11 LIMIT 1
        dump($res);
    }

    public function select()
    {
        //查询满足条件的多条记录
        $res = Db::table('staff')
            ->field(['name as 姓名','salary as 工资'])
            ->where('salary', '>', '3000')
//            ->order('salary DESC')  //ASC由小到大  DESC  由大到小
            ->order('salary','asc')  //第二种写法
                ->limit(5)
            ->select();
        dump($res);
    }

    //新增操作
    public function insert()
    {
        //新增一条记录
//        $data=[
//            'name' => '处处留情',
//            'sex' => 0,
//            'age' => 79,
//            'salary' => 5300
//            ];

//        $num = Db::table('staff')->insert($data);
//        $id = Db::getLastInsID();
//        return $num ? '添加成功,$id='.$id : '没有记录被添加';
//          return $num ? '添加成功,$id='.$id : '没有记录被添加';

        //$data($data):将数据打包$option[]
        //insertGetId() == insert() + getLastInsID()


//        $id = Db::table('staff')
//            ->insertGetId($data);
//        return $id ? '添加成功,$id='.$id : '没有记录被添加';

//        $id = Db::table('staff')
//            ->data($data)
//            ->insert();
//        $id = Db::getLastInsID();
//        return $id ? '添加成功,$id='.$id : '没有记录被添加';


        //新增多条记录:insertAll()
        $data = [
          ['name' => '张飞','sex'=>0,'age'=>50,'salary'=>7800],
          ['name' => '关羽','sex'=>0,'age'=>60,'salary'=>9000],
          ['name' => '刘备','sex'=>0,'age'=>70,'salary'=>6900]
        ];

        //执行语句
           $num = Db::table('staff')
            ->data($data)
            ->insertAll();
        //返回结果
        return $num ? '添加成功,'.$num .'条记录' : '没有记录被添加';
    }

    //更新操作
    public function update()
    {
        //不允许无条件更新
        //将工资大于等于8000的减500元
//        $num = Db::table('staff')
//            ->where('salary','>=',8000)
//            ->data(['salary'=>Db::raw('salary-500')])   //Db::raw('salary')获取原始数据
//            ->update();

        $num = Db::table('staff')
            ->update(['sex'=>1,'staff_id'=>39]);  //update([更新内容,更新条件])

        return $num ? '更新成功,'.$num .'条记录' : '没有记录被更新';

    }

    //删除操作
    public function delete()
    {
        //不允许无条件删除:delete()
        //返回的是受影响的记录条数

//        $num = Db::table('staff')->delete(39);  //delete(删除主键)
//        $num = Db::table('staff')
//            ->delete([7,8,9]);        //删除多个指定id
        $num = Db::table('staff')
            ->where('salary','>=',9000)
            ->delete();

        $num =Db::table('staff')->delete(true);   //清空数据表

        return $num ? '删除成功,'.$num .'条记录' : '没有记录被删除';

        //删除数据使用软删除实现,用更新模拟删除
    }
}

在实际开发中,不在控制器操作数据库,采用模型来操作数据库


Correcting teacher:查无此人Correction time:2019-04-16 09:39:49
Teacher's summary:完成的不错。说的对,好的项目都不在控制器里操作数据库。继续加油。

Release Notes

Popular Entries