今天看了看为Api而生的ThinkPHP5.0, 感觉还不错,想玩好这个框架,就先抛弃之前对thinkphp的认识吧,这个变化很大,
路由什么的有点麻烦,就先用默认的;路由,看了看ORM,模型和控制器等等~~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
<?php namespace app\index\controller; use app\common\model\produce; use app\common\model\test; use think\Controller; use think\Request; use think\Db; use think\Cache; use think\Session; use think\Cookie; use think\File; class Index extends Controller { public function _empty($name){ return $this->showCity($name); } protected function showCity($name) { //和$name这个城市相关的处理 return '当前城市' . $name; } public function edit($id) { return 'edit:'.$id; } public function Request() { Request::instance()->has('id','get'); Request::instance()->has('name','post'); } public function index(){ $data = db('produce')->where('produce_id','>',1)->find(); $data = Db::table('produce')->where('produce_id','>',1)->find(); $this->find(); } public function insert(){ $data = ['mobile' => 'bar', 'openid' => 'foo']; $resullt = Db::table('user')->insert($data); p($resullt); } public function update(){ Db::table('user') ->where('id', 1) ->update([ 'login_time' => ['exp','now()'], 'login_times' => ['exp','login_times+1'], ]); //更新某一个 Db::table('think_user') ->where('id',1) ->setField('name', 'thinkphp'); } public function del(){ 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(); } public function find(){ Db::table('user') ->where('name','like','%thinkphp') ->where('status',1) ->find(); //SELECT * FROM `user` WHERE `name` LIKE '%thinkphp' AND `status` = 1 LIMIT 1 Db::table('user') ->where('name','like','%thinkphp') ->whereOr('openid','like','%thinkphp') ->find(); //SELECT * FROM `user` WHERE `name` LIKE '%thinkphp' OR `openid` LIKE '%thinkphp' LIMIT 1 } public function make(){ Db::table('user') ->where('uid',1) ->field('id,name,email') ->find(); Db::table('user') ->where('status',1) ->where('id',1) ->delete(); Db::listen(function($sql, $time, $explain){ // 记录SQL echo $sql. ' ['.$time.'s]'; // 查看性能分析结果 dump($explain); }); } public function count(){ Db::table('think_user')->count(); Db::table('think_user')->count('id'); Db::table('think_user')->max('score'); Db::table('think_user')->where('score>0')->min('score'); Db::table('think_user')->avg('score'); Db::table('think_user')->sum('score'); } public function shiwu(){ // 启动事务 Db::startTrans(); try{ Db::table('think_user')->find(1); Db::table('think_user')->delete(1); // 提交事务 Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); } } public function cache(){ Cache::set('name',123,3600); // name自增(步进值为1) Cache::inc('name'); // name自增(步进值为3) Cache::inc('name',3); // name自减(步进值为1) Cache::dec('name'); // name自减(步进值为3) Cache::dec('name',3); //删除缓存 Cache::rm('name'); //获取并删除缓存 Cache::pull('name'); //不存在则写入缓存数据后返回 Cache::remember('name',function(){ return time(); }); //缓存标签 Cache::tag('tag')->set('name1','value1'); Cache::tag('tag')->set('name2','value2'); // 或者批量设置缓存标签 Cache::set('name1','value1'); Cache::set('name2','value2'); Cache::tag('tag',['name1','name2']); // 清除tag标签的缓存数据 Cache::clear('tag'); } public function session(){ Session::set('name','thinkphp'); Session::get('name'); // 赋值(当前作用域) Session::set('name','thinkphp'); // 赋值think作用域 Session::set('name','thinkphp','think'); // 判断(当前作用域)是否赋值 Session::has('name'); // 判断think作用域下面是否赋值 Session::has('name','think'); // 取值(当前作用域) Session::get('name'); // 取值think作用域 Session::get('name','think'); // 删除(当前作用域) Session::delete('name'); // 删除think作用域下面的值 Session::delete('name','think'); // 指定当前作用域 Session::prefix('think'); } public function cookie(){ // 设置Cookie 有效期为 3600秒 Cookie::set('name','value',3600); // 设置cookie 前缀为think_ Cookie::set('name','value',['prefix'=>'think_','expire'=>3600]); // 支持数组 Cookie::set('name',[1,2,3]); //判断 Cookie::has('name'); // 判断指定前缀的cookie值是否存在 Cookie::has('name','think_'); //获取 Cookie::get('name'); // 获取指定前缀的cookie值 Cookie::get('name','think_'); //删除cookie Cookie::delete('name'); // 删除指定前缀的cookie Cookie::delete('name','think_'); } public function file(){ } } |
下面是模型有关的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
<?php namespace app\common\model; use think\Model; class produce extends Model{ protected $pk = 'produce_id'; protected $autoWriteTimestamp = true;//自动写入时间戳 public function __construct() { parent::__construct(); } public function index(){ $user = produce::create([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com' ]); echo $user->name; } public function updates(){ //取出id为1的数据,并且更新后保存 $user = produce::get(1); $user->name = 'thinkphp'; $user->email = 'thinkphp@qq.com'; $user->save(); $user = new produce; // save方法第二个参数为更新条件 $user->save([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com' ],['id' => 1]); $user = new produce(); // 过滤post数组中的非数据表字段数据 $user->allowField(true)->save($_POST,['id' => 1]); $user = new produce(); // post数组中只有name和email字段会写入 $user->allowField(['name','email'])->save($_POST, ['id' => 1]); } public function statisUpdate(){ //模型支持静态方法直接更新数据 produce::where('id', 1) ->update(['name' => 'thinkphp']); produce::update(['id' => 1, 'name' => 'thinkphp']); //闭包更新 $user = new produce; $user->save(['name' => 'thinkphp'],function($query){ // 更新status值为1 并且id大于10的数据 $query->where('status', 1)->where('id', '>', 10); }); } public function del(){ produce::destroy(1); // 支持批量删除多个数据 produce::destroy('1,2,3'); // 或者 produce::destroy([1,2,3]); // 删除状态为0的数据 produce::destroy(['status' => 0]); //还支持使用闭包删除 produce::destroy(function($query){ $query->where('id','>',10); }); //通过数据库类的查询条件删除 produce::where('id','>',10)->delete(); } public function get_once(){ //取出主键为1的数据 $user = produce::get(1); echo $user->name; // 使用数组查询 $user = produce::get(['name' => 'thinkphp']); // 使用闭包查询 $user = produce::get(function($query){ $query->where('name', 'thinkphp'); }); echo $user->name; } public function juhe_find(){ //静态查询 produce::count(); produce::where('status','>',0)->count(); produce::where('status',1)->avg('score'); produce::max('score'); //动态查询 $user = new produce; $user->count(); $user->where('status','>',0)->count(); $user->where('status',1)->avg('score'); $user->max('score'); } //自动更新时间戳 public function timeSet(){ $user = new produce(); $user->name = 'THINKPHP'; $user->save(); echo $user->create_time; // 输出类似 1462545582 echo $user->update_time; // 输出类似 1462545582 } //自动完成 protected $auto = ['name', 'ip', 'password']; protected $insert = ['status' => 1]; protected $update = []; protected function setNameAttr($value) { return strtolower($value); } protected function setIpAttr() { return request()->ip(); } protected function setPasswordAttr($value) { return md5($value); } //自动完成结束 public function autoOver(){ } } |
任何一个方法使用前,请确保在头部命名空间的地方引入~