<?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(){
}
}