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的调试,为解决此问题,本人在此杂技一把了 方案一: ...
随机推荐
- LINQ 组合查询 和分页查询的使用
前端代码 <%@ Page Language="C#" AutoEventWireup="true" Debug="true" Cod ...
- BZOJ 2851: 极限满月 虚树 or 树链的并
2851: 极限满月 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 170 Solved: 82[Submit][Status][Discuss] ...
- MyBatis01 Idea中搭建MyBatis开发环境
项目结构 POM模板 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- PAT (Basic Level) Practise (中文)- 1015. 德才论 (25)
http://www.patest.cn/contests/pat-b-practise/1015 宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡 ...
- javaweb基础(14)_jsp的原理
一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...
- Node.js 中文学习资料和教程导航
这篇文章来自 Github 上的一位开发者收集整理的 Node.js 中文学习资料和教程导航.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念,它的目标是帮助程 ...
- Mybatis学习记录(1)
1.Mybatis介绍 Mybatis是apache的一个开源项目iBatis,Mybatis是一个优秀的持久层框架,他对jdbc的操作数据库的过程进行封装,使开发者只需要关注sql本身,不需 ...
- 12_1_Annotation注解
12_1_Annotation注解 1. 什么是注解 Annotation是从JDK5.0开始引入的新技术. Annotation的作用: 不是程序本身,可以对程序作出解释.可以被其他程序(比如,编译 ...
- oc 数据类型转换
NSNumber转NSString: 假设现有一NSNumber的变量A,要转换成NSString类型的B 方法如下: NSNumberFormatter* numberFormatter = [[N ...
- POJ-1961-Period(ZOJ-2177)
这题是最短循环节,然后我们尝试小于字符串长度的所有长度,如果符合,我们就输出. 如果它等于0,说明它不循环,因为之前并没有重复的,如果i%(i-next[i])==0说明它是循环的,然后除一下得到周期 ...