diff --git a/Library/Think/Model.php b/Library/Think/Model.php index 4f22cf1d..a4254b33 100644 --- a/Library/Think/Model.php +++ b/Library/Think/Model.php @@ -690,6 +690,30 @@ class Model { } } + /** + * SQL查询 + * @access public + * @param string $sql SQL指令 + * @param array $bind 参数绑定 + * @return mixed + */ + public function query($sql,$bind=[]) { + $sql = strtr($sql,['__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix]); + return $this->db->query($sql,$bind); + } + + /** + * 执行SQL语句 + * @access public + * @param string $sql SQL指令 + * @param array $bind 参数绑定 + * @return false | integer + */ + public function execute($sql,$bind=[]) { + $sql = strtr($sql,['__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix]); + return $this->db->execute($sql,$bind); + } + /** * 设置数据对象值 * @access public @@ -1001,7 +1025,7 @@ class Model { * @return Model */ public function map($map){ - $this->map = array_merge($this->map,$map); + $this->map = $map; return $this; } } \ No newline at end of file diff --git a/Traits/Think/Model/Query.php b/Traits/Think/Model/Query.php index e67cd495..9c67853b 100644 --- a/Traits/Think/Model/Query.php +++ b/Traits/Think/Model/Query.php @@ -1,114 +1,92 @@ - -// +---------------------------------------------------------------------- - -namespace Traits\Think\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查询 - * @access public - * @param string $sql SQL指令 - * @param array $bind 参数绑定 - * @return mixed - */ - public function query($sql,$bind=[]) { - return $this->db->query($sql,$bind); - } - - /** - * 执行SQL语句 - * @access public - * @param string $sql SQL指令 - * @param array $bind 参数绑定 - * @return false | integer - */ - public function execute($sql,$bind=[]) { - return $this->db->execute($sql,$bind); - } - - /** - * 解析SQL语句 - * @access public - * @param string $sql SQL指令 - * @param boolean $parse 是否需要解析SQL - * @return string - */ - public function parseSql($sql,$parse) { - // 分析表达式 - if(true === $parse) { - $options = $this->_parseOptions(); - $sql = $this->db->parseSql($sql,$options); - }elseif(is_array($parse)){ // SQL预处理 - $sql = vsprintf($sql,$parse); - }else{ - $sql = strtr($sql,['__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix]); - } - return $sql; - } - - /** - * 批处理执行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 true; - } + +// +---------------------------------------------------------------------- + +namespace Traits\Think\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语句 + * @access public + * @param string $sql SQL指令 + * @param boolean $parse 是否需要解析SQL + * @return string + */ + public function parseSql($sql,$parse) { + // 分析表达式 + if(true === $parse) { + $options = $this->_parseOptions(); + $sql = $this->db->parseSql($sql,$options); + }elseif(is_array($parse)){ // SQL预处理 + $sql = vsprintf($sql,$parse); + }else{ + $sql = strtr($sql,['__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix]); + } + return $sql; + } + + /** + * 批处理执行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 true; + } } \ No newline at end of file