#438
展开↯#439
作者:广西河池市
即使try里包含continue,break,return,try块结束后,finally块也会执行。
文章:finally代码块 发表时间:2018-09-01, 16:28:53
#440
作者:广西河池市
我觉得当try,finally都有return时,不能叫做覆盖,因为try中的return只执行了表达式,然后放在某个空间,但是并没有执行return(因为一旦执行return程序就结束了),然后执行finally里面的代码,接着finally中遇到return直接结束了程序,所以应该说是执行了try中return的表达式,而被finally中的return提前结束比较好,并不是finally中的return覆盖了try中的return。 #,广西河池市,2018-09-01,16:26:57, 对于基本类型而言 是值传递 所以2被缓存的是值 而不是变量的地址 再return出去就是2 #,广西河池市,2018-09-01,16:27:14, String类型不可变 也就是String a = "abc";后,存储"abc"字符串的地址的内容不可变,而a变量的重新赋值是通过指向另一块内存地址来实现的。同int型变量。String型变量暂存的是"abc"的存储地址,因此不会发生变化。你可以用StringBuffer、StringBuilder等可变字符类型来对比。
文章:finally代码块 发表时间:2018-09-01, 16:26:11
#441
作者:广西河池市
Mysql查询某字段值重复的数据
查询user表中,user_name字段值重复的数据及重复次数
查询user表中,user_name字段值重复的数据及重复次数
select user_name,count(*) as count from user group by user_name having count>1;
Run code
Cut to clipboard
文章:PHP高并发Redis MySQL重复插入测试笔记 发表时间:2018-08-31, 20:11:30
#443
作者:广西河池市
第一:如果线程1插入订单的过程中,线程2再去插入此时线程2会返回“订单已存在”的提示 ,然后问题是如果线程1插入失败,那么最终结果是数据库里并没有这个订单,但是线程2的用户却看到了“订单已存在” 这样的“错误”提示
第二:关于数据库事务的问题,在ssh这种框架里,经常出现配置的事务是在service方法执行前开启事务,在方法执行完以后提交事务,如果是这种情况的话,那还是会出现数据重复的问题,比如线程1 执行完 MemcacheUtil.del(key); 方法时 此时线程1的事务还没有提交(也就是说数据库里还没有相应的数据),但是缓存里的key已经被删掉了,与此同时线程2看不到数据库里的数据,缓存里的数据也没有 #,广西河池市,2018-08-31,14:53:49, 多台服务器部署,MemcacheUtil 要保证是全局的,不然也有问题
第二:关于数据库事务的问题,在ssh这种框架里,经常出现配置的事务是在service方法执行前开启事务,在方法执行完以后提交事务,如果是这种情况的话,那还是会出现数据重复的问题,比如线程1 执行完 MemcacheUtil.del(key); 方法时 此时线程1的事务还没有提交(也就是说数据库里还没有相应的数据),但是缓存里的key已经被删掉了,与此同时线程2看不到数据库里的数据,缓存里的数据也没有
文章:处理高并发情况下的DB插入 发表时间:2018-08-31, 14:53:36
#444
作者:广西河池市宜州市
PHP中利用redis实现消息队列处理高并发请求
#,广西河池市宜州市,2018-08-30,18:09:43, #,广西河池市宜州市,2018-08-30,18:17:30,
先将商品库存存入队列:
客户执行下单操作:
将请求存入redis
为了模拟多个用户的请求,使用一个for循环替代
//redis数据入队操作
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
for($i=0;$i<50;$i++){
try{
$redis->LPUSH('click',rand(1000,5000));
}catch(Exception $e){
echo $e->getMessage();
}
}
在后台进行数据处理
守护进程
//redis数据出队操作,从redis中将请求取出
$redis = new Redis();
$redis->pconnect('127.0.0.1',6379);
while(true){
try{
$value = $redis->LPOP('click');
if(!$value){
break;
}
//var_dump($value)."\n";
/*
* 利用$value进行逻辑和数据处理
*/
}catch(Exception $e){
echo $e->getMessage();
}
}
Run code
Cut to clipboard
//关于redis事务的案例
header("content-type:text/html;charset=utf-8");
$redis = new redis();
$redis->connect('localhost', 6379);
//$result = $redis->connect('localhost', 6379);
//$redis = Yii::app()->redis;
$redis->set("testName","33");
//$mywatchkey = $redis->get("mywatchkey");
$mywatchkey = $redis->get('testName');
//($test);exit;
$rob_total = 100; //抢购数量
if($mywatchkey<$rob_total){
$redis->watch("testName");
$redis->multi();
//设置延迟,方便测试效果。
sleep(5);
//插入抢购数据
$redis->hSet("testName","user_id_".mt_rand(1, 9999),time());
$redis->set("testName",$mywatchkey+1);
$rob_result = $redis->exec();
if($rob_result){
$mywatchlist = $redis->hGetAll("testName");
echo "抢购成功!<br/>";
echo "剩余数量:".($rob_total-$mywatchkey-1)."<br/>";
echo "用户列表:<pre>";
var_dump($mywatchlist);
}else{
echo "手气不好,再抢购!";exit;
}
}
Run code
Cut to clipboard
先将商品库存存入队列:
<?php
$store=1000; //商品库存
$redis=new Redis();
$result=$redis->connect('127.0.0.1',6379);
$res=$redis->llen('goods_store');
for($i=0; $i<$store; $i++){
$redis->lpush('goods_store',1);
}
echo $redis->llen('goods_store');
?>
Run code
Cut to clipboard
客户执行下单操作:
$redis=new Redis();
$result=$redis->connect('127.0.0.1',6379);
$count = $redis->lpop('goods_store');
if(!$count){
echo '抢购失败!';
return;
}
Run code
Cut to clipboard
文章:php redis 学习笔记 发表时间:2018-08-30, 18:08:32
#446
展开↯#447
作者:广西南宁市
老板发工资给我,不是因为我为公司做事,而是因为我为了这份工作,没能为自己做事。
不是因为公司得到了什么,而是因为我失去了什么。
所以公司给我的那份工资,严格来说不是工资,是赔偿。
其中分别就是,你出粮给我,我要多谢你。
你赔偿给我,你要加多一句“对不起”。
——黄子华
雇佣关系而已,我时刻提醒自己
第1次你给我发错(多)了,我原谅你。
第2次你又发错(少)了,怎么能继续原谅你?
一个很老的笑话
15830371000
文章:@意见反馈/技术支持/伊网/安企网 发表时间:2017-09-14, 11:48:07
#448
展开↯#449
作者:广西河池市宜州市
安装nginx #,广西河池市宜州市,2018-08-01,10:10:20, 输入命令./nginx -t
看到如下显示nginx.conf syntax is ok
nginx.conf test is successful
说明配置文件正确 #,广西河池市宜州市,2018-08-01,10:11:12,
在启动命令-c前加-t #,广西河池市宜州市,2018-08-10,23:22:32, #,广西河池市宜州市,2018-08-21,15:18:10,
Linux下ftp服务可以通过搭建vsftpd服务来实现
执行 systemctl start vsftpd.service 启动vsftp服务,然后可以通过命令: systemctl status vsftpd.service 查看ftp服务的运行状态
#,广西河池市宜州市,2018-08-21,15:53:09, sftp和ftp区别在于:文件的安全性
FTP是一种文件传输协议,一般是为了方便数据共享的。包括一个FTP服务器和多个FTP客户端。FTP客户端通过FTP协议在服务器上下载资源。
SFTP协议是在FTP的基础上对数据进行加密,使得传输的数据相对来说更安全。但是这种安全是以牺牲效率为代价的。
SFTP的传输效率比FTP要低。
//进入nginx目录
cd nginx
Run code
Cut to clipboard
看到如下显示nginx.conf syntax is ok
nginx.conf test is successful
说明配置文件正确
wget -O wordpress.zip http://www.linuxde.net/download.aspx?id=1080
Centos出现-bash: unzip: command not found的解决办法
利用unzip命令解压缩的时候,出现-bash:
unzip: command not found的错误。
unzip——命令没有找到,其原因肯定是没有安装unzip。
利用一句命令就可以解决了。
命令是:
yum install -y unzip zip
安装成功后就可以使用unzip命令
Run code
Cut to clipboard
Linux下ftp服务可以通过搭建vsftpd服务来实现
执行 systemctl start vsftpd.service 启动vsftp服务,然后可以通过命令: systemctl status vsftpd.service 查看ftp服务的运行状态
systemctl stop vsftpd.service
Run code
Cut to clipboard
FTP是一种文件传输协议,一般是为了方便数据共享的。包括一个FTP服务器和多个FTP客户端。FTP客户端通过FTP协议在服务器上下载资源。
SFTP协议是在FTP的基础上对数据进行加密,使得传输的数据相对来说更安全。但是这种安全是以牺牲效率为代价的。
SFTP的传输效率比FTP要低。
文章:LINUX安装NGINX笔记 发表时间:2018-06-14, 17:49:17
#450
作者:广西南宁市
文章:程序员编程常用网页工具集[游戏] 发表时间:2017-07-29, 10:26:00
#451
作者:广西河池市宜州市
文章:@意见反馈/技术支持/伊网/安企网 发表时间:2018-08-02, 15:09:59
#452
作者:广西河池市宜州市
到底是“学术研究无禁区”,还是“思想道德无底线” #,广西河池市宜州市,2018-08-17,09:14:03, 集权可以跑的很快,但刹不住车
文章:@意见反馈/技术支持/伊网/安企网 发表时间:2018-08-17, 09:10:02
#453
作者:广西河池市宜州市
october CMS 模板
变量:
标签:
过滤器:
功能:
this.page:
this.layout:
this.param:
this.environment
{%partial%}
{%content%}
{%占位符%}
变量:
{{variable}}
{{isAjax?'Yes':'No'}}
{{'Your name:' ~ name}}
Run code
Cut to clipboard
标签:
{%tag%}
{%if stormCloudComing%}
Stay inside
{else %}
Gooutside and play
{%endif%}
{%set activePage='blog'%}
Run code
Cut to clipboard
过滤器:
{{‘string’|filter}}
{{price |currency('USD')}}
{{'October Glory'|upper|replace({'October':'Morning'})}}
Run code
Cut to clipboard
功能:
{{function()}}
{{dump(variable)}}
Run code
Cut to clipboard
this.page:
//布局:
{{this.page.layout}}
//id:文件名称和文件夹名称转换为css友好标识符
<body class="page-{{this.page.id}}">
//标题
<h1>{{this.page.title}}</h1>
//描述
<p>{{ this.page.description }}</p>
//meta_title
<title>{{ this.page.meta_title }}</title>
//meta_description
<meta name="description" content="{{ this.page.meta_description }}">
//隐:隐藏的页面只能由登录的后端用户访问。
{% if this.page.hidden %}
<p>Note to other admins: We are currently working on this page.</p>
{% endif %}
Run code
Cut to clipboard
this.layout:
//属性:您可以通过访问当前的布局对象,this.layout并返回该对象Cms\Classes\Layout
//ID:
<body class="layout-{{ this.layout.id }}">
//描述
<meta name="description" content="{{ this.layout.description }}">
this.theme:
//属性 :this.theme将提供直接访问表单字段值,由任何主题定制定义。它本身也具有以下特性
//ID
<body class="theme-{{ this.theme.id }}">
//配置
<meta name="description" content="{{ this.theme.config.description }}">
Run code
Cut to clipboard
this.param:
//您可以通过this.param它访问当前的URL参数,并返回一个PHP数组
//访问页面参数
//如何访问tab页面中的URL参数。
url = "/account/:tab"
==
{% if this.param.tab == 'details' %}
<p>Here are all your details</p>
{% elseif this.param.tab == 'history' %}
<p>You are viewing a blast from the past</p>
{% endif %}
//如果参数名称也是一个变量,那么可以使用数组语法
url = "/account/:post_id"
==
{% set name = 'post_id' %}
<p>The post ID is: {{ this.param[name] }}</p>
Run code
Cut to clipboard
this.environment
//您可以通过访问当前环境对象,this.environment并返回引用当前环境配置的字符串
//如果网站在测试环境中运行,以下示例将显示横幅:
{% if this.environment == 'test' %}
<div class="banner">Test Environment</div>
{% endif %}
Run code
Cut to clipboard
{%partial%}
{% partial "footer" %}
{% partial "sidebar/menu" %}
{% set tabName = "profile" %}
{% partial tabName %}
% partial "blog-posts" posts=posts %}
{% partial "location" city="Vancouver" country="Canada" %}
<p>Country: {{ country }}, city: {{ city }}.</p>
Run code
Cut to clipboard
{%content%}
{% content "contacts.htm" %}
{% content "sidebar/content.htm" %}
{% content "readme.txt" %}
{% content "changelog.md" %}
{% put sidebar %}
{% content 'sidebar-content.htm' %}
{% endput %}
{% content "welcome.htm" name=user.name %}
{% content "location.htm" city="Vancouver" country="Canada" %}
<p>Country: {country}, city: {city}.</p>
{% content "welcome.htm" likes=[
{name:'Dogs'},
{name:'Fishing'},
{name:'Golf'}
] %}
<ul>
{likes}
<li>{name}</li>
{/likes}
</ul>
Run code
Cut to clipboard
{%占位符%}
{% placeholder name %}
{% put name %}
<p>Place this text in the name placeholder</p>
{% endput %}
{% placeholder sidebar default %}
<p><a href="/contacts">Contact us</a></p>
{% endplaceholder %}
{% put sidebar %}
<p><a href="/services">Services</a></p>
{% default %}
{% endput %}
//检测占位符是否存在
{% if placeholder('sidemenu') %}
<!-- Markup for a page with a sidebar -->
<div class="row">
<div class="col-md-3">
{% placeholder sidemenu %}
</div>
<div class="col-md-9">
{% page %}
</div>
</div>
{% else %}
<!-- Markup for a page without a sidebar -->
{% page %}
{% endif %}
//自定义属性
{% placeholder ordering title="Ordering information" type="text" %}
{% placeholder ordering default title="Ordering information" type="text" %}
There is no ordering information for this product.
{% endplaceholder %}
Run code
Cut to clipboard
文章:OctoberCms学习笔记中文文档翻译 发表时间:2018-08-14, 16:55:00
#454
作者:广西河池市宜州市
模仿网易梦幻西游游戏西游记师徒四人行走动画css效果 gif背景动画图片 唐僧 孙悟空 猪八戒 沙和尚 西天取经
<style>
.west-01 {
position: absolute;
width: 200px;
height: 180px;
left: 50%;
margin-left: -398px;
top: 352px;
background-image: url(https://out.img.pan.lizhenqiu.com/FqDMTm_8I03UBd6Lx-cY9FaIB99m);
background-repeat: no-repeat;
-webkit-animation: play01 1.4s steps(8) infinite;
-moz-animation: play01 1.4s steps(8) infinite;
-ms-animation: play01 1.4s steps(8) infinite;
animation: play01 1.4s steps(8) infinite
}
@-webkit-keyframes play01 {
0% {
background-position: 0 0
}
100% {
background-position: -1600px 0
}
}
@-moz-keyframes play01 {
0% {
background-position: 0 0
}
100% {
background-position: -1600px 0
}
}
@-o-keyframes play01 {
0% {
background-position: 0 0
}
100% {
background-position: -1600px 0
}
}
@-ms-keyframes play01 {
0% {
background-position: 0 0
}
100% {
background-position: -1600px 0
}
}
@keyframes play01 {
0% {
background-position: 0 0
}
100% {
background-position: -1600px 0
}
}
.west-02 {
position: absolute;
width: 200px;
height: 180px;
left: 50%;
margin-left: -198px;
top: 356px;
background-image: url(https://out.img.pan.lizhenqiu.com/Fjr4nM2HtaFF-XM-oabkVJBL-U1z);
background-repeat: no-repeat;
-webkit-animation: play02 1.4s steps(8) infinite;
-moz-animation: play02 1.4s steps(8) infinite;
-ms-animation: play02 1.4s steps(8) infinite;
animation: play02 1.4s steps(8) infinite
}
@-webkit-keyframes play02 {
0% {
background-position: 0 0
}
100% {
background-position: -1600px 0
}
}
@-moz-keyframes play02 {
0% {
background-position: 0 0
}
100% {
background-position: -1600px 0
}
}
@-o-keyframes play02 {
0% {
background-position: 0 0
}
100% {
background-position: -1600px 0
}
}
@-ms-keyframes play02 {
0% {
background-position: 0 0
}
100% {
background-position: -1600px 0
}
}
@keyframes play02 {
0% {
background-position: 0 0
}
100% {
background-position: -1600px 0
}
}
.west-03 {
position: absolute;
width: 170px;
height: 240px;
left: 50%;
margin-left: -11px;
top: 326px;
background-image: url(https://out.img.pan.lizhenqiu.com/Ft1ZQTkZbB535lBwZ4vjVhWU5ol6);
background-repeat: no-repeat;
-webkit-animation: play03 1.4s steps(8) infinite;
-moz-animation: play03 1.4s steps(8) infinite;
-ms-animation: play03 1.4s steps(8) infinite;
animation: play03 1.4s steps(8) infinite
}
@-webkit-keyframes play03 {
0% {
background-position: 0 0
}
100% {
background-position: -1360px 0
}
}
@-moz-keyframes play03 {
0% {
background-position: 0 0
}
100% {
background-position: -1360px 0
}
}
@-o-keyframes play03 {
0% {
background-position: 0 0
}
100% {
background-position: -1360px 0
}
}
@-ms-keyframes play03 {
0% {
background-position: 0 0
}
100% {
background-position: -1360px 0
}
}
@keyframes play03 {
0% {
background-position: 0 0
}
100% {
background-position: -1360px 0
}
}
.west-04 {
position: absolute;
width: 210px;
height: 200px;
left: 50%;
margin-left: 198px;
top: 372px;
background-image: url(https://out.img.pan.lizhenqiu.com/FjTycCEEtOfP7JYjlvAybcZVFYqh);
background-repeat: no-repeat;
-webkit-animation: play04 1.4s steps(8) infinite;
-moz-animation: play04 1.4s steps(8) infinite;
-ms-animation: play04 1.4s steps(8) infinite;
animation: play04 1.4s steps(8) infinite
}
@-webkit-keyframes play04 {
0% {
background-position: 0 0
}
100% {
background-position: -1680px 0
}
}
@-moz-keyframes play04 {
0% {
background-position: 0 0
}
100% {
background-position: -1680px 0
}
}
@-o-keyframes play04 {
0% {
background-position: 0 0
}
100% {
background-position: -1680px 0
}
}
@-ms-keyframes play04 {
0% {
background-position: 0 0
}
100% {
background-position: -1680px 0
}
}
@keyframes play04 {
0% {
background-position: 0 0
}
100% {
background-position: -1680px 0
}
}
</style>
<div class="west-01"></div>
<div class="west-02"></div>
<div class="west-03"></div>
<div class="west-04"></div>
Run code
Cut to clipboard
文章:模仿优酷首页的一些前端布局 发表时间:2018-08-09, 11:08:14
#455
作者:广西河池市宜州市
git 换行符LF与CRLF转换问题
#,广西河池市宜州市,2018-08-09,10:48:28, 解决crlf 和 lf不同带来的冲突问题
crlf 和 lf 是文本换行的不同方式:
crlf: "\r\n", windows系统的换行方式
lf: "\n", Linux系统的换行方式
他们之间的不同经常会导致不同会导致使用不同系统的同事之间的代码冲突问题。
在你使用git拉取代码的时候,git会自动将代码当中与你当前系统不同的换行方式转化成你当前系统的换行方式,从而造成这种冲突。
window系统解决办法:
1. 修改git全局配置,禁止git自动将lf转换成crlf, 命令:
git config --global core.autocrlf false
2. 修改编辑器的用户配置,例如vscode
"files.eol": "\n", // 文件换行使用lf方式
一、背景
在各操作系统下,文本文件所使用的换行符是不一样的。UNIX/Linux 使用的是 0x0A(LF),早期的 Mac OS 使用的是0x0D(CR),后来的 OS X 在更换内核后与 UNIX 保持一致了。但 DOS/Windows 一直使用 0x0D0A(CRLF)作为换行符。Git提供了一个“换行符自动转换”功能。这个功能默认处于“自动模式”,当你在签出文件时,它试图将 UNIX 换行符(LF)替换为 Windows 的换行符(CRLF);当你在提交文件时,它又试图将 CRLF 替换为 LF。Git 的“换行符自动转换”功能听起来似乎很智能、很贴心,因为它试图一方面保持仓库内文件的一致性(UNIX 风格),一方面又保证本地文件的兼容性(Windows 风格)。但遗憾的是,这个功能是有 bug 的,而且在短期内都不太可能会修正。
二、解决方案
1.Git设置
git config --global core.autocrlf false
git config --global core.safecrlf true
含义:
AutoCRLF
#提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true
#提交时转换为LF,检出时不转换
git config --global core.autocrlf input
#提交检出均不转换
git config --global core.autocrlf false
SafeCRLF
#拒绝提交包含混合换行符的文件
git config --global core.safecrlf true
#允许提交包含混合换行符的文件
git config --global core.safecrlf false
#提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn
2.IDE设置使用UNIX换行符
IDEA的设置File -> Settings
Editor -> Code Style
Line separator (for new lines) ,选择:Unix and OS X (\n)
对已使用Windows换行符的文件,可以使用Sublime Text打开,
View->Line Endings,选Unix,保存;
Run code
Cut to clipboard
crlf 和 lf 是文本换行的不同方式:
crlf: "\r\n", windows系统的换行方式
lf: "\n", Linux系统的换行方式
他们之间的不同经常会导致不同会导致使用不同系统的同事之间的代码冲突问题。
在你使用git拉取代码的时候,git会自动将代码当中与你当前系统不同的换行方式转化成你当前系统的换行方式,从而造成这种冲突。
window系统解决办法:
1. 修改git全局配置,禁止git自动将lf转换成crlf, 命令:
git config --global core.autocrlf false
2. 修改编辑器的用户配置,例如vscode
"files.eol": "\n", // 文件换行使用lf方式
文章:GitHub代码管理代码托管 发表时间:2018-08-09, 10:48:10
#456
作者:广西河池市宜州市
octobercms数据库使用
检索结果
models:
//运行select查询
$users = Db::select(‘select * from users where active = ?‘, [1]);
传递给该select方法的第一个参数是原始SQL查询,而第二个参数是需要绑定到查询的任何参数绑定。通常,这些是where子句约束的值。参数绑定提供了针对SQL注入的保护。
该select方法将始终返回一个array结果。数组中的每个结果将是一个PHP stdClass对象,允许您访问结果的值:
foreach ($users as $user) {
echo $user->name;
}
//使用命名绑定
$results = Db::select(‘select * from users where id = :id‘, [‘id‘ => 1]);
//运行一个insert语句
要执行一个insert语句,你可以使用insert在Db立面上的方法。像这样select,这个方法将原始SQL查询作为第一个参数,绑定作为第二个参数:
Db::insert(‘insert into users (id, name) values (?, ?)‘, [1, ‘Joe‘]);
//运行更新语句
该update方法应用于更新数据库中的现有记录。该语句受影响的行数将由方法返回:
$affected = Db::update(‘update users set votes = 100 where name = ?‘, [‘John‘]);
//运行delete语句
该delete方法应用于从数据库中删除记录。喜欢update,将返回删除的行数:
$deleted = Db::delete(‘delete from users‘);
//运行一般声明
某些数据库语句不应返回任何值。对于这些类型的操作,您可以使用立面上的statement方法Db
Db::statement(‘drop table users‘);
//多个数据库连接
当使用多个连接时,您可以通过立面上的connection方法访问每个连接Db。在name传递给connection方法应该对应于您列出的其中一个连接config/database.php组态文件中
$users = Db::connection(‘foo‘)->select(...);
$pdo = Db::connection()->getPdo();
//数据库事务
Db::transaction(function () {
Db::table(‘users‘)->update([‘votes‘ => 1]);
Db::table(‘posts‘)->delete();
});
//手动使用交易
如果您想手动开始事务并完全控制回滚和提交,您可以使用立面上的beginTransaction方法Db:
Db::beginTransaction();
您可以通过以下rollBack方法回滚事务:
Db::rollBack();
最后,您可以通过以下commit方法提交事务:
Db::commit();
//数据库事件
如果要接收应用程序执行的每个SQL查询,可以使用该listen方法。此方法对于记录查询或调试很有用。
Db::listen(function($sql, $bindings, $time) {
//
});
//查询记录
启用查询日志记录时,将记录所有已为当前请求运行的查询的内存。调用该enableQueryLog方法启用此功能
Db::connection()->enableQueryLog();
要获取执行查询的数组,可以使用以下getQueryLog方法:
$queries = Db::getQueryLog();
然而,在某些情况下,例如当插入大量行时,这可能导致应用程序使用多余的内存。要禁用日志,您可以使用以下disableQueryLog方法:
Db::connection()->disableQueryLog();
Run code
Cut to clipboard
检索结果
//从表中检索所有行--get来看一下表中的所有记录
$users = Db::table(‘users‘)->get();
//通过访问列作为对象的属性来访问每列的值:
foreach ($users as $user) {
echo $user->name;
}
//从表中检索单个行/列
$user = Db::table(‘users‘)->where(‘name‘, ‘John‘)->first();
echo $user->name;
//如果您甚至不需要整行,您可以使用该value方法从记录中提取单个值。此方法将直接返回列的值:
$email = Db::table(‘users‘)->where(‘name‘, ‘John‘)->value(‘email‘);
//分块结果从一张桌子
Db::table(‘users‘)->chunk(100, function($users) {
foreach ($users as $user) {
//
}
});
//您可以通过以下方式返回false来阻止进一步处理的块Closure:
Db::table(‘users‘)->chunk(100, function($users) {
// Process the records...
return false;
});
//检索列值列表
//如果要检索包含单列值的数组,则可以使用该lists方法
$titles = Db::table(‘roles‘)->lists(‘title‘);
foreach ($titles as $title) {
echo $title;
}
//您还可以为返回的数组指定自定义键列:
$roles = Db::table(‘roles‘)->lists(‘title‘, ‘name‘);
foreach ($roles as $name => $title) {
echo $title;
}
//骨料
$users = Db::table(‘users‘)->count();
$price = Db::table(‘orders‘)->max(‘price‘);
$price = Db::table(‘orders‘)
->where(‘is_finalized‘, 1)
->avg(‘price‘);
//选择
$users = Db::table(‘users‘)->select(‘name‘, ‘email as user_email‘)->get();
//该distinct方法允许您强制查询返回不同的结果:
$users = Db::table(‘users‘)->distinct()->get();
$query = Db::table(‘users‘)->select(‘name‘);
$users = $query->addSelect(‘age‘)->get();
//原始表达式
$users = Db::table(‘users‘)
->select(Db::raw(‘count(*) as user_count, status‘))
->where(‘status‘, ‘<>‘, 1)
->groupBy(‘status‘)
->get();
//加盟
$users = Db::table(‘users‘)
->join(‘contacts‘, ‘users.id‘, ‘=‘, ‘contacts.user_id‘)
->join(‘orders‘, ‘users.id‘, ‘=‘, ‘orders.user_id‘)
->select(‘users.*‘, ‘contacts.phone‘, ‘orders.price‘)
->get();
$users = Db::table(‘users‘)
->leftJoin(‘posts‘, ‘users.id‘, ‘=‘, ‘posts.user_id‘)
->get();
Db::table(‘users‘)
->join(‘contacts‘, function ($join) {
$join->on(‘users.id‘, ‘=‘, ‘contacts.user_id‘)->orOn(...);
})
->get();
Db::table(‘users‘)
->join(‘contacts‘, function ($join) {
$join->on(‘users.id‘, ‘=‘, ‘contacts.user_id‘)
->where(‘contacts.user_id‘, ‘>‘, 5);
})
->get();
//where子句
$users = Db::table(‘users‘)->where(‘votes‘, ‘=‘, 100)->get();
$users = Db::table(‘users‘)->where(‘votes‘, 100)->get();
$users = Db::table(‘users‘)
->where(‘votes‘, ‘>=‘, 100)
->get();
$users = Db::table(‘users‘)
->where(‘votes‘, ‘<>‘, 100)
->get();
$users = Db::table(‘users‘)
->where(‘name‘, ‘like‘, ‘T%‘)
->get();
//“或”声明
$users = Db::table(‘users‘)
->where(‘votes‘, ‘>‘, 100)
->orWhere(‘name‘, ‘John‘)
->get();
//“之间”之间的陈述
$users = Db::table(‘users‘)
->whereBetween(‘votes‘, [1, 100])->get();
$users = Db::table(‘users‘)
->whereNotBetween(‘votes‘, [1, 100])
->get();
//“在哪里”声明
$users = Db::table(‘users‘)
->whereIn(‘id‘, [1, 2, 3])
->get();
$users = Db::table(‘users‘)
->whereNotIn(‘id‘, [1, 2, 3])
->get();
//“null”语句
$users = Db::table(‘users‘)
->whereNull(‘updated_at‘)
->get();
$users = Db::table(‘users‘)
->whereNotNull(‘updated_at‘)
->get();
//排序
$users = Db::table(‘users‘)
->orderBy(‘name‘, ‘desc‘)
->get();
//分组
$users = Db::table(‘users‘)
->groupBy(‘account_id‘)
->having(‘account_id‘, ‘>‘, 100)
->get();
//该havingRaw方法可用于将原始字符串设置为having子句的值。例如,我们可以找到销售额超过$ 2,500的所有部门:
$users = Db::table(‘orders‘)
->select(‘department‘, Db::raw(‘SUM(price) as total_sales‘))
->groupBy(‘department‘)
->havingRaw(‘SUM(price) > 2500‘)
->get();
//限制和抵消
//要限制从查询返回的结果数,或者在查询(OFFSET)中跳过给定数量的结果,可以使用skip和take方法:
$users = Db::table(‘users‘)->skip(10)->take(5)->get();
//插入
Db::table(‘users‘)->insert(
[‘email‘ => ‘john@example.com‘, ‘votes‘ => 0]
);
Db::table(‘users‘)->insert([
[‘email‘ => ‘taylor@example.com‘, ‘votes‘ => 0],
[‘email‘ => ‘dayle@example.com‘, ‘votes‘ => 0]
]);
//自动递增ID
$id = Db::table(‘users‘)->insertGetId(
[‘email‘ => ‘john@example.com‘, ‘votes‘ => 0]
);
//更新
Db::table(‘users‘)
->where(‘id‘, 1)
->update([‘votes‘ => 1]);
//递增/递减
Db::table(‘users‘)->increment(‘votes‘);
Db::table(‘users‘)->increment(‘votes‘, 5);
Db::table(‘users‘)->decrement(‘votes‘);
Db::table(‘users‘)->decrement(‘votes‘, 5);
Db::table(‘users‘)->increment(‘votes‘, 1, [‘name‘ => ‘John‘]);
//删除
Db::table(‘users‘)->delete();
Db::table(‘users‘)->where(‘votes‘, ‘<‘, 100)->delete();
Db::table(‘users‘)->truncate();
//悲观锁定
Db::table(‘users‘)->where(‘votes‘, ‘>‘, 100)->sharedLock()->get();
Db::table(‘users‘)->where(‘votes‘, ‘>‘, 100)->lockForUpdate()->get();
//缓存查询
//持久缓存
$users = Db::table(‘users‘)->remember(10)->get();
//内存缓存
Db::table(‘users‘)->get(); // Result from database
Db::table(‘users‘)->get(); // Result from database
Model::all(); // Result from database
Model::all(); // Result from in-memory cache
//您可以使用enableDuplicateCache或disableDuplicateCache方法启用或禁用重复缓存
Db::table(‘users‘)->enableDuplicateCache()->get();
//如果查询存储在缓存中,则当使用insert,update,delete或truncate语句时,它将自动被清除。但是您可以使用该flushDuplicateCache方法手动清除缓存
Db::table(‘users‘)->enableDuplicateCache()->get();
Run code
Cut to clipboard
models:
//介绍
plugins/
acme/
blog/
models/
user/ <=== Model config directory
columns.yaml <=== Model config files
fields.yaml <==^
User.php <=== Model class
Plugin.php
//定义模型
namespace Acme\Blog\Models;
use Model;
class Post extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = ‘acme_blog_posts‘;
}
//支持的属性
class User extends Model
{
protected $primaryKey = ‘id‘;
public $exists = false;
protected $dates = [‘last_seen_at‘];
public $timestamps = true;
protected $jsonable = [‘permissions‘];
protected $guarded = [‘*‘];
}
//首要的关键
class Post extends Model
{
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = ‘id‘;
}
//时间戳
class Post extends Model
{
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
}
//如果您需要自定义时间戳的格式,请设置$dateFormat模型上的属性。此属性确定日期属性如何存储在数据库中,以及当模型序列化为数组或JSON时的格式:
class Post extends Model
{
/**
* The storage format of the model‘s date columns.
*
* @var string
*/
protected $dateFormat = ‘U‘;
}
//值存储为JSON
class Post extends Model
{
/**
* @var array Attribute names to encode and decode using JSON.
*/
protected $jsonable = [‘data‘];
}
//访问列值
foreach ($flights as $flight) {
echo $flight->name;
}
//添加附加约束
$flights = Flight::where(‘active‘, 1)
->orderBy(‘name‘, ‘desc‘)
->take(10)
->get();
//集合
foreach ($flights as $flight) {
echo $flight->name;
}
//分块结果
Flight::chunk(200, function ($flights) {
foreach ($flights as $flight) {
//
}
});
//检索单个模型
// Retrieve a model by its primary key
$flight = Flight::find(1);
// Retrieve the first model matching the query constraints
$flight = Flight::where(‘active‘, 1)->first();
//找不到例外
$model = Flight::findOrFail(1);
$model = Flight::where(‘legs‘, ‘>‘, 100)->firstOrFail();
Route::get(‘/api/flights/{id}‘, function ($id) {
return Flight::findOrFail($id);
});
//检索聚合
$count = Flight::where(‘active‘, 1)->count();
$max = Flight::where(‘active‘, 1)->max(‘price‘);
//基本插入
$flight = new Flight;
$flight->name = ‘Sydney to Canberra‘;
$flight->save();
//基本更新
$flight = Flight::find(1);
$flight->name = ‘Darwin to Adelaide‘;
$flight->save();
//还可以针对与给定查询匹配的任意数量的模型执行更新。在这个例子中,所有的航班active,有一个destination的San Diego将被标记为延迟:
Flight::where(‘is_active‘, true)
->where(‘destination‘, ‘Perth‘)
->update([‘delayed‘ => true]);
//质量分配
class Flight extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [‘name‘];
}
//一旦我们赋予了属性质量分配,我们可以使用该create方法在数据库中插入新的记录。该create方法返回保存的模型实例:
$flight = Flight::create([‘name‘ => ‘Flight 10‘]);
//其他创作方法
$flight = Flight::make([‘name‘ => ‘Flight 10‘]);
// Functionally the same as...
$flight = new Flight;
$flight->fill([‘name‘ => ‘Flight 10‘]);
// Retrieve the flight by the attributes, otherwise create it
$flight = Flight::firstOrCreate([‘name‘ => ‘Flight 10‘]);
// Retrieve the flight by the attributes, or instantiate a new instance
$flight = Flight::firstOrNew([‘name‘ => ‘Flight 10‘]);
//删除模型
$flight = Flight::find(1);
$flight->delete();
//按键删除现有的模型
Flight::destroy(1);
Flight::destroy([1, 2, 3]);
Flight::destroy(1, 2, 3);
//通过查询删除模型
$deletedRows = Flight::where(‘active‘, 0)->delete();
//查询范围
class User extends Model
{
/**
* Scope a query to only include popular users.
*/
public function scopePopular($query)
{
return $query->where(‘votes‘, ‘>‘, 100);
}
/**
* Scope a query to only include active users.
*/
public function scopeActive($query)
{
return $query->where(‘is_active‘, 1);
}
}
//利用查询范围
$users = User::popular()->active()->orderBy(‘created_at‘)->get();
//动态范围
class User extends Model
{
/**
* Scope a query to only include users of a given type.
*/
public function scopeApplyType($query, $type)
{
return $query->where(‘type‘, $type);
}
}
//现在您可以在调用范围时传递参数:
$users = User::applyType(‘admin‘)->get();
Run code
Cut to clipboard
文章:OctoberCms学习笔记中文文档翻译 发表时间:2018-08-07, 09:21:33
1、不管有木有出现异常,finally块中代码都会执行;
2、当try和catch中有return时,finally仍然会执行;
3、finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值),所以函数返回值是在finally执行前确定的;
4、finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值。
如果finally中有return语句,那么程序就return了,所以finally中的return是一定会被return的,
编译器把finally中的return实现为一个warning。
在转去之前,try中先把要返回的结果存放到不同于x的局部变量中去,执行完finally之后,在从中取出返回结果,
因此,即使finally中对变量x进行了改变,但是不会影响返回结果。
它应该使用栈保存返回值。