tp5.1框架学习之数据库操作

it2022-05-08  11

原生sql操作 先连接数据库

<?php namespace app\index\controller; use think\Controller; use think\Db as mdb; class Db extends Controller { public function db() { //前缀,助手函数读取配置文件的配置 // echo config('database.prefix'); //查 //$sql = "select * from " . config('database.prefix') . "articles "; //$sql .= "where id=:id"; //$res = mdb::query($sql, ['id'=>103]); //增 //添加一条数据 //$sql = "insert into ". config('database.prefix') . "articles "; //$sql.="(title,desn,body) values(:title,:desc,:body)"; //$res = mdb::execute($sql,['title'=>'我是新增title','desc'=>'我是描述','body'=>'我是body']); //改 //$sql = "update ". config('database.prefix') . "articles "; //$sql.= "set title=:title where id=:id"; //$res = mdb::execute($sql,['title'=>'我是改后标题','id'=>218]); //删 //$sql = "delete from ".config('database.prefix') . "articles "; //$sql.="where id=:id"; //$res = mdb::execute($sql, ['id'=>218]); //dump($res); } }

添加数据(能使用助手函数的都推荐使用助手函数)

<?php namespace app\index\controller; use think\Controller; class Db2 extends Controller { public function index() { //添加数据 //$data= ['title'=>'我是新增title','desn'=>'我是描述','body'=>'我是body']; //多条数据 // $data2 = [ // ['title' => '我是新增title1', 'desn' => '我是描述', 'body' => '我是body'], // ['title' => '我是新增title2', 'desn' => '我是描述', 'body' => '我是body'] // ]; //$res = db('articles')->data($data)->insert(); //添加一条数据,并返回新增的主键id //$res = db('articles')->insertGetId($data); //插入多条数据 //$res = db('articles')->insertAll($data2); dump($res); } }

更新数据

<?php namespace app\index\controller; use think\Controller; use think\Db; class Db2 extends Controller { public function index() { $res =db('articles')->where('id','=','224')->update([ 'title'=>'新改的标题' ]); 访问量每次访问 click字段 +1 $res =db('articles')->where('id','=','224')->update([ 'click'=>Db::raw('click+1') ]); dump($res); } }

删除数据

# 根据主键删除 Db::table('think_user')->delete(1); Db::table('think_user')->delete([1,2,3]); # 条件删除 Db::table('think_user')->where('id',1)->delete(); Db::table('think_user')->where('id','<',10)->delete(); # 无条件删除所有数据 小心去用 Db::name('user')->delete(true); # 软删除数据 使用delete_time字段标记删除 逻辑删除 # 软删除,必须要表中要delete_time字段 Db::name('user') ->where('id', 1) ->useSoftDelete('delete_time',time()) ->delete();

查询数据  

//根据主键ID查询数据 返回一维数组 $res = db('articles')->find(224); $res = db('articles')->where('id',224)->find(); //where还可以多条件,没有查询分组 SQL:SELECT * FROM `tp_articles` WHERE `id` > 100 AND `click` > 100 $res = db('articles')->where('id','>',100)->where('click','>',100)->select(); //等价如上写法 $where = [ 'id' => ['>', 100], 'click' => ['>', 100] ]; $obj = new Where($where); $res = db('articles')->where($obj)->select(); //where还支持匿名函数 ,有查询分组 SQL:SELECT * FROM `tp_articles` WHERE ( `id` > 100 AND `click` > 100 ) $res = db('articles')->where(function (Query $query){ $query->where('id','>',100)->where('click','>',100); })->select(); //和上面是一样 //SQL: SELECT * FROM `tp_articles` WHERE ( `id` > 100 AND `click` > 100 ) OR ( `title` LIKE 'a%' ) $res = db('articles')->where(function (Query $query){ $query->where('id','>',100)->where('click','>',100); })->whereOr(function (Query $query){ $query->where('title','like','a%'); })->select(); // 此用法一般用于 接口,要是没有查询到数据就抛出异常 $res = db('articles')->where('id',985)->findOrFail(); $res = db('articles')->where('id','>',500)->selectOrFail(); // 排序和分页 //$res = db('articles')->order('id','desc')->limit(0,2)->select(); // 获取指定的字段的值 //$ret = db('articles')->where('id',210)->value('title'); // 获取指定的列 #$ret = db('articles')->column('title'); //这个的意思是返回数组的键是id ,值是title #$ret = db('articles')->column('title','id'); //意思是在所有查询数据的title前面都加上 '世界你好----' // 获取器 当前值 数据源 $ret = db('articles')->withAttr('title',function ($value,$data){ return '世界你好---'.$value; })->where('id','>',200)->select();

模型操作

php think make:model 模块名/模型名(首字母大写) php think make:model 模型名(首字母大写) 一般是这么用 # 非必须,意思是你的模型类名和表名不一致或者主键不是为id,假如文章模型,主键是aid之类的需要设置 protected $pk = 'uid'; # 设置主键名称 protected $table = 'think_user'; #设置当前模型对应的完整数据表名称 # 模型的实例化 //引入命名空间 use app\common\model\Articles; //new一个模型 $model = new Articles(); //助手函数实例化 $model = model('articles');

模型添加数据  

#添加数据 $user = new User; $user->save([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com' ]); # 过滤post数组中的非数据表字段数据,模型对象中才有的方法 $user->allowField(true)->save($_POST); $user->allowField([要插入的字段,要插入的字段])->save($_POST); #通过静态方法添加数据 $user = User::create([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com' ]); # 添加多条记录 $user = new User; $list = [ ['name'=>'thinkphp','email'=>'thinkphp@qq.com'], ['name'=>'onethink','email'=>'onethink@qq.com'] ]; $user->saveAll($list);

模型更新数据  

# 模型更新数据方法一 $user = new User; // save方法第二个参数为更新条件 $user->save([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com' ],['id' => 1]); $user = new User; // 过滤post数组中的非数据表字段数据 $user->allowField(true)->save($_POST,['id' => 1]); # 静态方法更新数据(推荐使用) User::where('id', 1)->update(['name' => 'thinkphp']);

模型删除数据  

# 方法1 $user = User::get(1); $user->delete(); # 方法2 User::destroy(1); User::destroy([1,2,3]);

模型操作软删除

<?php namespace app\common\model; use think\Model; use think\model\concern\SoftDelete; class Articles extends Model { use SoftDelete; //这是软删除的标记字段,数据表中要添加一个字段 ka int 长度为10 默认为null protected $deleteTime = 'ka'; //官网有提示这个玩意,千万不要加,加了作用就没有了,这是个大坑 //只需要配置上面两个就可以实现软删除了 //protected $defaultSoftDelete = 0; } //软删除,会在你的字段直接添加当前时间戳,作为标记 dump(Articles::destroy(223)); //软删除之后通过的数据查找出来为NULL dump(Articles::get(223)); //只查询软删除的数据 dump(Articles::onlyTrashed()->find()); //返回二维数组,软删除的数据 dump(Articles::onlyTrashed()->select()); //恢复被软删除的数据 $art = Articles::onlyTrashed()->find(223); $art->restore();

 


模型查询数据  

# 查询单条记录 $user = User::where('name', 'thinkphp')->find(); # 查询多条记录 $list = User::where('status', 1)->limit(3)->order('id', 'asc')->select(); # 获取某个字段或者某个列的值 // 获取某个用户的积分 User::where('id',10)->value('score'); // 获取某个列的所有值 User::where('status',1)->column('name'); # 动态查询 // 根据name字段查询用户 $user = User::getByName('thinkphp');

trait(重点,php5.4.0之后出现):解决php想要多继承问题,其实php是可以多继承的

<?php trait A { public function aa() { return 'aaa'; } public function bb() { return 'BBBBB'; } } trait B { public function bb() { return 'bb'; } } class User { public function hello() { return 'hello'; } } class VipUser extends User { use A,B{ //用B里面的bb方法替代A里面的bb方法,总结:谁在前面调用谁的方法 B::bb insteadof A; //如果此时还想调用A里面的方法怎么办?起别名即可 A::bb as Ab; } public function hello2() { return 'hello2'; } } $vip = new VipUser(); echo $vip->bb(); //bb echo $vip->Ab(); //BBBBB

 


模型获取器操作

 

//同时模型也支持,db类中的获取器方式 $ret = $articles->withAttr('title',function ($value,$data){ return '世界你好---'.$value; })->where('id','>',200)->select();

 


最新回复(0)