ThinkPHP5杂技(一)
Thinkphp5 assign 传递 " 时 ,前台收到的是 " 和ThinkPHP3.2不一样,3.2收到的是 ”,传递给js时
用的data.replace(new RegExp('"','g'),'"')替换回来
貌似不好,获取值的时候可以用 {$data.abc|raw} 解决传入参数"替换"问题
也就是如果前台传入的是json数据 后台用 { |raw}解决 Thinkphp5.1
在部分Base.php 控制器中 \think\Request::instance()->controller()根本调用不到
需要 \think\facade\Request::instance()->controller() 可以, 前台url链接指向其他控制器方法时可用
{foreach name="vo" item="val"}
<li id="{$val.title}">
<a href=" {:url($val.model.'/'.$val.action)}">{$val.title}</a>
</li>
{/foreach}
当前台输出输出数据时
输出的效果是
网页源代码可以看到因果
想要的效果是
解决方式
浏览器开发者工具中
网页源代码中
总结:模板动态输出含标签的html代码,或者双引号时,默认输出结果会经过htmlentities()转义,结果不是我们想要的,解决办法时{XX|raw}
模板继承的时候第二种模板标签方式 replace="[XXX]" 不要用replace="{XXX}" 否则会意外多出 “/> Thinkphp5.1用Model类时,return数据
在控制器dump返回的数据时显示是一个数组————坑
array_merge()总是提示不是数组
用var_dump()打印Model返回值时发现,那是一个对象
想要Model返回array对象增加一个->toArray()
Coin::where('userid',$data)->find()->toArray();
get与param区别
Thinkphp5.1遇到的坑
某两个相似的时间戳前台用,已提交issure,已改进 githubIssure githubCode
输出
一个输出的是
有的输出就不正确,打印出变量查看时间戳是正确的
产看缓存中的输出
再追究
$time = 1508982277; //2017/10/26 9:44:37
var_dump(strtotime($time));
echo '<br>';
$time = 1508489607; //2017/10/20 16:53:27
var_dump(strtotime($time));
这结果!!
3.2是这么干的
模型关联删除问题
A hanMany B
public function test()
{
$user = A::get(1);
echo '<pre>';
$temp = $user->products; var_dump($temp); //得到的是think\model\Collection对象
var_dump($user->products()->select()); //得到的是think\model\Collection对象,$user->products()得到的是hasMany对象
var_dump($user->products()->where('id', 28)->find()); //得到B对象 var_dump($user->products()->where('id', 28)->delete()); //数据库中数据已删除
var_dump($user->products()->select()); //id = 28 的数据已删除
var_dump($temp); //id = 28 的数据依旧存在 }
object(think\model\Collection)#99 (1) {
["items":protected]=>
array(3) {
[0]=>
object(app\user\model\ShopCar)#102 (2) {
["data"]=>
array(5) {
["id"]=>
int(27)
["产品id"]=>
int(2)
["数量"]=>
int(1)
["编号"]=>
string(3) "gao"
["操作时间"]=>
int(1509757081)
}
["relation"]=>
array(0) {
}
}
[1]=>
object(app\user\model\ShopCar)#101 (2) {
["data"]=>
array(5) {
["id"]=>
int(28)
["产品id"]=>
int(5)
["数量"]=>
int(1)
["编号"]=>
string(3) "gao"
["操作时间"]=>
int(1509757086)
}
["relation"]=>
array(0) {
}
}
[2]=>
object(app\user\model\ShopCar)#100 (2) {
["data"]=>
array(5) {
["id"]=>
int(29)
["产品id"]=>
int(6)
["数量"]=>
int(1)
["编号"]=>
string(3) "gao"
["操作时间"]=>
int(1509757861)
}
["relation"]=>
array(0) {
}
}
}
}
object(think\model\Collection)#95 (1) {
["items":protected]=>
array(3) {
[0]=>
object(app\user\model\ShopCar)#16 (2) {
["data"]=>
array(5) {
["id"]=>
int(27)
["产品id"]=>
int(2)
["数量"]=>
int(1)
["编号"]=>
string(3) "gao"
["操作时间"]=>
int(1509757081)
}
["relation"]=>
array(0) {
}
}
[1]=>
object(app\user\model\ShopCar)#96 (2) {
["data"]=>
array(5) {
["id"]=>
int(28)
["产品id"]=>
int(5)
["数量"]=>
int(1)
["编号"]=>
string(3) "gao"
["操作时间"]=>
int(1509757086)
}
["relation"]=>
array(0) {
}
}
[2]=>
object(app\user\model\ShopCar)#97 (2) {
["data"]=>
array(5) {
["id"]=>
int(29)
["产品id"]=>
int(6)
["数量"]=>
int(1)
["编号"]=>
string(3) "gao"
["操作时间"]=>
int(1509757861)
}
["relation"]=>
array(0) {
}
}
}
}
object(app\user\model\ShopCar)#16 (2) {
["data"]=>
array(5) {
["id"]=>
int(28)
["产品id"]=>
int(5)
["数量"]=>
int(1)
["编号"]=>
string(3) "gao"
["操作时间"]=>
int(1509757086)
}
["relation"]=>
array(0) {
}
}
int(1)
object(think\model\Collection)#87 (1) {
["items":protected]=>
array(2) {
[0]=>
object(app\user\model\ShopCar)#90 (2) {
["data"]=>
array(5) {
["id"]=>
int(27)
["产品id"]=>
int(2)
["数量"]=>
int(1)
["编号"]=>
string(3) "gao"
["操作时间"]=>
int(1509757081)
}
["relation"]=>
array(0) {
}
}
[1]=>
object(app\user\model\ShopCar)#89 (2) {
["data"]=>
array(5) {
["id"]=>
int(29)
["产品id"]=>
int(6)
["数量"]=>
int(1)
["编号"]=>
string(3) "gao"
["操作时间"]=>
int(1509757861)
}
["relation"]=>
array(0) {
}
}
}
}
object(think\model\Collection)#99 (1) {
["items":protected]=>
array(3) {
[0]=>
object(app\user\model\ShopCar)#102 (2) {
["data"]=>
array(5) {
["id"]=>
int(27)
["产品id"]=>
int(2)
["数量"]=>
int(1)
["编号"]=>
string(3) "gao"
["操作时间"]=>
int(1509757081)
}
["relation"]=>
array(0) {
}
}
[1]=>
object(app\user\model\ShopCar)#101 (2) {
["data"]=>
array(5) {
["id"]=>
int(28)
["产品id"]=>
int(5)
["数量"]=>
int(1)
["编号"]=>
string(3) "gao"
["操作时间"]=>
int(1509757086)
}
["relation"]=>
array(0) {
}
}
[2]=>
object(app\user\model\ShopCar)#100 (2) {
["data"]=>
array(5) {
["id"]=>
int(29)
["产品id"]=>
int(6)
["数量"]=>
int(1)
["编号"]=>
string(3) "gao"
["操作时间"]=>
int(1509757861)
}
["relation"]=>
array(0) {
}
}
}
}
模型可以直接当成普通方式查询数据,当join()后返回的不再是模型对象,而是array数据
ShopCar::where([['a.编号', '=', $userName], ['b.状态', '=', '使用']])->alias('a')
->join(['商城产品' => 'b'], 'a.产品id = b.id', 'LEFT')->column('a.产品id,a.数量,b.价格,b.pv,b.可订购数量,b.名称,b.重量,(b.可订购数量-a.数量) as numLeft', 'a.id');
行锁lock(true) 或者lock(false)什么也不做
ThinkPHP5.1
thinkphp\library\think\route\dispatch\Module.php P108 用到 is_callable()方法

if (is_callable([$instance, $action])) {
// 执行操作方法
$call = [$instance, $action];
// 自动获取请求变量
$vars = $this->app->config('app.url_param_type')
? $this->app['request']->route()
: $this->app['request']->param();
} elseif (is_callable([$instance, '_empty'])) {
// 空操作
$call = [$instance, '_empty'];
$vars = [$actionName];
} else {
// 操作不存在
throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()');
} $this->app['hook']->listen('action_begin', $call); return Container::getInstance()->invokeMethod($call, $vars);
}

但是当 $instance中有 __call 方法时,is_callable([$instance, $action]) 返回的总为true,需要注意,貌似Thinkphp5中反射导致的__call方法无效(个人临时理解)

colin 03-Oct-2010 08:30 //代码块来自PHP手册 I haven't seen anyone note this before, but is_callable will correctly determine the existence of methods made with __call. The method_exists function will not. Example:
<?php class Test { public function testing($not = false) {
$not = $not ? 'true' : 'false';
echo "testing - not: $not<br/>";
} public function __call($name, $args) {
if(preg_match('/^not([A-Z]\w+)$/', $name, $matches)) {
$fn_name = strtolower($matches[1]);
if(method_exists($this, $fn_name)) {
$args[] = true; // add NOT boolean to args
return call_user_func_array(array($this, $matches[1]), $args);
}
}
die("No method with name: $name<br/>");
} } $t = new Test();
$t->testing();
$t->notTesting(); echo "exists: ".method_exists($t, 'notTesting').'<br/>';
echo "callable: ".is_callable(array($t, 'notTesting')); ?> Output: testing - not: false
testing - not: true
exists:
callable: 1

Thinphp5手册上说让用中文表名,中文字段,没有详细说明
个人可以想到的地方,
一是 pdo操作占位符的时候没特殊处理,见
二是 中文表名得在model主动声明我这个model对应的是哪个表,英文表明一般可省略
三是Model返回的全是对象,字段实际上是对象的属性 ->id来调用,如果把id换成中文名称,怪怪的,还没见过谁的代码真用中文做变量。虽然也可以用 $obj['id’]查看属性的值
支付宝验证规则 邮箱/手机号/淘宝会员名
// 邮箱/手机号/淘宝会员名
'aliPay|c' => 'require|length:3,25',
'aliPay|d' => ['regex'=>'/^\w+((@\w{1,4}.\w{1,4})|(\w*))$/'],
觉得还比较理想
20171110
开发的项目中有这么个需求,对数据库中获取的数据进行加工。3.2的时候用的在控制器中获取数据库,然后用控制器中的方法修改数据。现在5.1尝试用模型的获取器。但是发现
中的$data是一个数组,也就是不能用模型关联中的数据。那可不行。
尝试后用$this对象访问便可
数据库/模型 返回类型
(new AModel)->saveAll($list) 返回的是插入的条数,ep:int(1)
ThinkPHP5杂技(一)的更多相关文章
- ThinkPHP5杂技(二)
不要使用数据库查询嵌套 if (!$listA = Db::name('coin') ->field('id,symbol') ->where('id', 'IN', logic('All ...
- TODO:macOS上ThinkPHP5和Semantic-UI集成
TODO:macOS上ThinkPHP5和Semantic-UI集成 1. 全局安装 (on OSX via homebrew)Composer 是 homebrew-php 项目的一部分 2. 把X ...
- ThinkPHP5 助手函数
对于ThinkPHP5.0以前的版本,助手函数全部是单字母函数,但到ThinkPHP5之后,使用如下函数来代替单字母函数: 最常用: /** * 实例化Model * @param string $n ...
- 高性能PHP框架thinkphp5.0.0 Beta发布-为API开发而设计
ThinkPHP V5.——为API开发而设计的高性能框架 ThinkPHP5..0版本是一个颠覆和重构版本,采用全新的架构思想,引入了很多的PHP新特性,优化了核心,减少了依赖,实现了真正的惰性加载 ...
- 吐槽THINKPHP5命令行
thinkphp,作为国内开源框架,一直在使用和学习. 但是实在忍不住想要吐槽一下他的开发文档,和 对初学者的不友好,建议刚接触MVC思想的人 还是尽量去使用其他框架当入门. 现在来吐槽一下think ...
- 【thinkphp5】【THINKCMG】标签部分开发(一)
最近打算开一个自己的CMS后台轮子,框架选择了thinkphp 5 (别问我为什么选择这个框架)然后想边开发边记录一下,方便自己方便他人 进入正题 1.数据库设计 一共三张表 post文章表这个就不贴 ...
- ThinkPHP5.0完全开发手册
http://www.kancloud.cn/manual/thinkphp5/118006 www WEB部署目录(或者子目录) ├─composer.json composer定 ...
- ThinkPHP5中Session的使用
由于用惯了ThinkPHP之前的版本,一想到要用Session就直接用$_SESSION来存取,今天看了ThinkPHP5的手册,才发现原来这么用时不安全滴.ThinKPHP5对Session进行了封 ...
- 杂技之sharpdevelop调试aps.net
背景: 本人笔记本电脑不给力,vs打开实在太慢,因此考虑使用sharpdevelop,但sharpdevelop有点麻烦事,就是不支持asp.net的调试,为解决此问题,本人在此杂技一把了 方案一: ...
随机推荐
- 洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths
题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...
- pod install Pull is not possible because you have unmerged files.
http://stackoverflow.com/questions/21474536/podfile-gives-an-error-on-install A bug was found in lib ...
- GPnP profile内容
<?xml version="1.0" encoding="UTF-8"?> <gpnp:GPnP-Profile Version=&quo ...
- input输入大于0的小数和整数
<input onkeyup="num(this)"onbeforepaste="num(this)"> <script src='jquer ...
- Codeforces Round #321 (Div. 2) C Kefa and Park(深搜)
dfs一遍,维护当前连续遇到的喵的数量,然后剪枝,每个统计孩子数量判断是不是叶子结点. #include<bits/stdc++.h> using namespace std; ; int ...
- [大坑]Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
报错tensorflow/stream_executor/cuda/cuda_dnn.cc:338] Could not create cudnn handle: CUDNN_STATUS_INTER ...
- React 官方脚手架 create-react-app快速生成新项目
进入新公司已经半年了,各个业务线,技术栈都已经熟悉,工作也已经游刃有余,决定慢下脚步,沉淀积累,回顾一下所用技术栈所包含的基本知识,以及再公司中的实战. 首先回顾新项目搭建 react脚手架目前使用较 ...
- hadoop相关资料集锦
1 Hadoop集群系列集锦http://www.cnblogs.com/xia520pi/archive/2012/04/08/2437875.html 2 Hadoop和MapReduce详解ht ...
- 剑指offer64 数据流中的中位数
priority_queue优先级队列,他的模板声明带有三个参数,priority_queue<Type, Container, Functional> Type 为数据类型, Conta ...
- Java中 Character方法练习:字符串中英文字母个数 5435abc54abc3AHJ5 正则:matches("[a-zA-Z0-9]{1}")
package com.swift; public class String_Letter_Number_Test { public static void main(String[] args) { ...