mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-05 22:52:49 +08:00
调整类的文件命名规范为psr-4
This commit is contained in:
80
library/traits/model/Query.php
Normal file
80
library/traits/model/Query.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace traits\model;
|
||||
|
||||
trait Query
|
||||
{
|
||||
|
||||
/**
|
||||
* 启动事务
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function startTrans()
|
||||
{
|
||||
$this->commit();
|
||||
$this->db->startTrans();
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交事务
|
||||
* @access public
|
||||
* @return boolean
|
||||
*/
|
||||
public function commit()
|
||||
{
|
||||
return $this->db->commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* 事务回滚
|
||||
* @access public
|
||||
* @return boolean
|
||||
*/
|
||||
public function rollback()
|
||||
{
|
||||
return $this->db->rollback();
|
||||
}
|
||||
|
||||
/**
|
||||
* 批处理执行SQL语句
|
||||
* 批处理的指令都认为是execute操作
|
||||
* @access public
|
||||
* @param array $sql SQL批处理指令
|
||||
* @return boolean
|
||||
*/
|
||||
public function patchQuery($sql = [])
|
||||
{
|
||||
if (!is_array($sql)) {
|
||||
return false;
|
||||
}
|
||||
// 自动启动事务支持
|
||||
$this->startTrans();
|
||||
try {
|
||||
foreach ($sql as $_sql) {
|
||||
$result = $this->execute($_sql);
|
||||
if (false === $result) {
|
||||
// 发生错误自动回滚事务
|
||||
$this->rollback();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 提交事务
|
||||
$this->commit();
|
||||
} catch (\think\exception $e) {
|
||||
$this->rollback();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user