TP6【更新时间:2020-03-04 13:08】
慕课链接:https://coding.imooc.com/class/407.html#Anchor
php-snowflake雪花算法(创建订单号唯一,重复几率极低)
https://github.com/godruoyi/php-snowflake
安装:
composer create-project topthink/think 自己定义的项目目录
更新:
composer update topthink/framework
php扩展:
composer require topthink/think-multi-app
composer require topthink/think-view // 视图
composer require topthink/think-captcha // 验证码
Controller只获取参数,
Service 进行页面处理
Model 只查询数据库
-----------------------现在的努力是为了小时候吹过的牛逼!Deng
declare(strict_types==1);
namespace .....
public function smsSend :object(){} :bool()
redis-server*
./redis-cli
set abd 1
get abd
"1"
-------------------------
查找单条记录:不存在抛异常
query()->findOrFail()
-------------------------
common/:
/business 只做逻辑处理(constroller只获取参数)
/lib ①:sms Alisms②:Num.php
/model 只查询数据库
/exception 公共的全局异常处理
/validate 只验证参数(可以放到controller)
-------------------------
getUserById
updateUserById()
-------------------------
事务
$this->model->startTrans();
$this->model->commit();
$this->model->rollback();
----------------------------
如何根据日志来分析当前qps高点和低点: cat access.log | awk '{print $4}' | uniq -c | sort -r
Mysql 建立索引
字段名 NORMAL BTREE
commonlibNum
declare(strict_types==1); class Num public status function getCode(int $len = 4) :int{ $code = rand(1000,9999); if ($len == 6) { $code = rand(1000000,999999); } }
php模板View
config/view.php
'tpl_replace_string'=>[
'{__STATIC_PATH}' => '/static/',
]
------------
域名/captcha
{:captcha_img(''),95,40}
{:url('verify/index')}
public function __construct(App $app) { $this->app = $app; $this->request = $this->app->request; // 控制器初始化 $this->initialize(); }
获取数据(param) // 不存在时,默认值为1, 只获取整数形数据 $data = $this->request->param('type_id',1,"intval");
二:
use thinkfacadeRequest;
public function index(Request $request)
{
dump($request->param('type_id'));
// 方法三:
dump(input('type_id'));
// 方法四
dump(request()->param('type_id'));
// 方法五:(门面模式)
dump(Request::param('type_id'));
}
public function __call($name,$param) { 返回控制器不存在/方法不存在 时 }
打印sql
fetchSql()->find()打印sql
二:
Db::getLastSql();exit;
模型ORM public function getStatusTextAttr($value,$data) {
$array = [
'1' => '已发布'
]
return $status[$data['status']] }
array_column('数组','数组中的key')
array_key($key), 只获取key,array
<?php
// 表示由数据库返回的可能记录集的数组
$a = array(
array(
'id' => 5698,
'first_name' => 'Bill',
'last_name' => 'Gates',
),
array(
'id' => 4767,
'first_name' => 'Steve',
'last_name' => 'Jobs',
)
);
$last_names = array_column($a, 'last_name')
;
print_r($last_names);
?>
-----------------------------
Array ( [0] => Gates [2] => Zuckerberg )
$where[] = ['pid','in',$pid] $where[] = ['pid','in',$pid] 尽量使用oRM $query->where('pid','<>',$pid)
public function __constroller()
{
$this->mobile = new Order();
}
public function searchAddTimeAttr($query, $value) { $query->whereBetweenTime('add_time',$value['0'],$value['1']); } public function getListData() { $this->withSearch($likeKeys, $data); }
$this->hasOne(Googs::class,'','[外键id==Goods的id]','自己对应id')
$this->model->with('goods')->find(1)
redis
pv+1
Cache::inc('mall_pv_'.$id)
使用redis保存购物车信息
num+之前保存的num
Cache::hser()
Cache::hget()
Cache::hGetAll()
// 数组数量
$count = Cache::hLen()
intval($count)
cart;购物车
保存redis值时,增加add_time,方便排序
$data = array_mcolumn($result,'add_time')
array_multisort($data,SORT_DESC,$result) true,false
ArrSortByKey
【分布式发号器】!!!
创建订单号:
雪花算法
$wordId = rand(1,1023);
$orderId = Snowflake::getInstance()->setWorkId($workId)->id();
.