Laravel事务操作
try {
works::insertGetId($adata);
workContents::insertGetId($diyAdata);
DB::commit();
}catch (Exception $e){
DB::rollback();
Log::debug('添加失败'.'$e->getMessage:'.$e->getMessage().'|getCode:'.$e->getCode());
return output_error('添加失败');
}
try {
works::insertGetId($adata);
workContents::insertGetId($diyAdata);
DB::commit();
}catch (Exception $e){
DB::rollback();
Log::debug('添加失败'.'$e->getMessage:'.$e->getMessage().'|getCode:'.$e->getCode());
return output_error('添加失败');
}
public function upload(Request $request){
$imgs = [];
if ($request->hasFile('upload_img')){
foreach ($request->file('upload_img') as $v){
$time = strtotime(date("Y-m-d 00:00:00"));
$path = $v->move(public_path().'/upload/'.$time,$v->hashName());
dump($path->getRealPath());die;
$imgs[]= asset($path);
}
return response()->json([
'errno'=>0,
'data'=>$imgs
]);
}else{
return response()->json([
'info'=>'没有图片'
]);
}
}
use SoftDeletes;//使用软删除
/**
public $hidden = ['hasOnetable','hasTwotable'];
public function hasOnetable(){
return $this->hasOne('App\hasOnetable','id','one_id');//第一个是关联表主键,第二个是本表的外键
}
public function hasTwotable(){
return $this->hasOne('App\hasTwotable','two_table_id','id');
}
const limit = 6;//设置一个常量
$taskId = $request->task_id;
$type = $request->type;
$map['task_id'] = $taskId;
$map['finished'] = 2;
$isAll = $request->is_all ? $request->is_all: 0;
$limit = $request->limit ? $request->limit : self::limit;
$data = UserHomeworks::where($map)
->when($type == 2 , function ($query) use ($type) {// 未复读
return $query->where('status',0);
})
->select('id','finished_time')
->orderBy('created_at', 'desc')
->with('hasOne') //这部是为了之后循环的时候不每一次都查库,渴求式加载
->when($isAll == 1 , function ($query) use ($isAll) {// 没有分页
return $query->get();
})
->when($isAll != 1 , function ($query) use ($isAll,$limit) {// 有分页
return $query->paginate($limit);
});
在同事交流中使用设计模式描述程序思想,效率要快的多.
初学者可以把设计模式理解成一种专业术语, 就是为了沟通开发交流无障碍的,
所以设计模式是必学的
github 代码地址: https://github.com/baisecaihong/GOF
单例模式:
生成原因:1全局变量是无法复用的,如果A程序的全局变量没有增加到B程序中,那么你就无法在B中执行代码
2全局变量将类捆绑与特定的环境,破坏了封装
作用:用单例模式改进全局变量,因为无法用错误类型的数据覆写一个单例
<?php
//单例模式
class danli{
private $value = array();
private static $instance;
public static function getInstance()
{
if(!(self::$instance)){
self::$instance = new danli();
}
return self::$instance;
}
private function __construct()
{
}
public function __clone()
{
// TODO: Implement __clone() method.
}
public function __wakeup()
{
// TODO: Implement __wakeup() method.
}
public function setValue($key,$val){
$this->value[$key] = $val;
}
public function getValue($key){
return $this->value[$key];
}
}
$danli = danli::getInstance();
$danli->setValue('name','白色彩虹');
unset($danli);
var_dump($danli);
$danli2 = danli::getInstance();
print $danli2->getValue('name');
解释下代码
创建danli类
声明私有化变量$value数据类型为array,这一步就阻止了设置value为其他类型的覆写
声明私有化静态变量$instance 静态变量只存在于函数作用域内,静态变量只存活在栈中,下次再调用这个函数的时候,该变量的值会保留下来 而且私有化只能类内部访问
声明公共静态方法getInstance
如果$instance为空就重新实例化自己创建一个danli类实例并且返回实例
私有化初始化方法
私有化克隆
私有化wakeup
这些是用来保持数据一致性,防止被克隆之后操作混乱
共有的设置value 和提取value
使用:$danli引用danli类中的getInstance;
设置value值
删除引用
这个时候打印下$danli返回Undefined variable: danli
说明已经删除了,但是之前设置的value的值还在
所以$danli2再引用一次,
打印值 可以打印出来
$instance是不能被类外部访问的只能通过getInstance进行访问,又因为getInstance方法是public且static的所以脚本的任何地方都可以调用 这就形成了单例模式,
alter table games add software_copyright VARCHAR(255) not Null default "" comment "软件著作权";
GET url
{
"query": {
"bool": {
"must": [
{"term": {
"task_id.keyword": {
"value": "130"
}
}},
{"term": {
"game_task_id": {
"value": "1"
}
}}
]
}
}
, "size": 20
}
首先在项目目录建一个777文件
AutoRunLog.txt
调用openLog方法传入想记录的数据
public function openLog($msg){
$myfile = fopen("AutoRunLog.txt", "a") or die("Unable to open file!");
$txt = date("Y-m-d H:i:s",time())." ".$msg."\n";
fwrite($myfile, $txt);
fclose($myfile);
}
最终展示结果
2018-10-12 16:41:07 $msg
2018-10-12 16:41:07 $msg
php版本7.1
<?php
$n=1;
$time=time();
while($n<100000000){
$n=$n+1;
}
echo time()-$time;
3秒
python版本3.7
8.27714991569519秒
python版本2.7
import time
n = 1
time_start = time.time()
while n < 100000000:
n = n+1
time_end = time.time()
print time_end - time_start
10.3120000362秒
斐波那契 php版本 7.1
<?php
/**
* Created by PhpStorm.
* User: dn
* Date: 2018/10/18
* Time: 17:17
*/
$startTime = time();
function f($a)
{
if ($a == 0 || $a == 1) {
return 1;
}
return f($a-1) + f($a-2);
}
for($i = 0; $i < 40; ++$i) {
echo f($i).' ';
}
$endTime = time();
echo $endTime-$startTime;
101秒
斐波那契 php版本 5.6
177秒
斐波那契 python2.7
182秒
今天在做jenkins的时候遇到了脚本中需要用到scp的情况
yum install expect
然后做个脚本
vi scp.sh
set timeout 60
spawn scp "百度scp格式"
expect "*password"
send "12345/r" #注意!!!如果密码中包含特殊字符 则需要在前面加 最后的r是必带的
expect eof
:wq
chmod +x scp.sh
./scp.sh
就齐活了