PHP Class Think\Db\Query

Show file Open project: top-think/framework Class Usage Examples

Protected Properties

Property Type Description
$bind 参数绑定
$builder 数据库驱动类型
$connection 数据库Connection对象实例
$info 数据表信息
$model 当前模型类名称
$name 当前数据表名称(不含前缀)
$options 查询参数
$pk 当前数据表主键
$prefix 当前数据表前缀
$table 当前数据表名称(含前缀)

Public Methods

Method Description
__call ( string $method, array $args ) : mixed 利用__call方法实现一些特殊的Model方法
__construct ( Connection $connection = null, string $model = '' ) 架构函数
alias ( mixed $alias ) 指定数据表别名
avg ( string $field = '*' ) : float | integer AVG查询
batchQuery ( array $sql = [] ) : boolean 批处理执行SQL语句 批处理的指令都认为是execute操作
bind ( mixed $key, mixed $value = false, integer $type = PDO::PARAM_STR ) 参数绑定
buildSql ( boolean $sub = true ) : string 创建子查询SQL
cache ( mixed $key = true, integer $expire = null, string $tag = null ) 查询缓存
chunk ( integer $count, callable $callback, string $column = null ) : boolean 分批数据返回处理
column ( string $field, string $key = '' ) : array 得到某个列的数组
comment ( string $comment ) 查询注释
commit ( ) : void 用于非自动提交状态下面的查询提交
connect ( mixed $config ) 切换当前的数据库连接
count ( string $field = '*' ) : integer COUNT查询
delete ( mixed $data = null ) : integer 删除记录
distinct ( string $distinct ) 指定distinct查询
execute ( string $sql, array $bind = [] ) : integer 执行语句
failException ( boolean $fail = true ) 设置查询数据不存在是否抛出异常
fetchClass ( string $class ) 指定数据集返回对象
fetchPdo ( boolean $pdo = true ) 不主动获取数据集
fetchSql ( boolean $fetch = true ) 获取执行的SQL语句
field ( mixed $field, boolean $except = false, string $tableName = '', string $prefix = '', string $alias = '' ) 指定查询字段 支持字段排除和指定数据表
find ( array | string | Query | Closure $data = null ) : array | false | PDOStatement | string | Model 查找单条记录
findOrFail ( array | string | Query | Closure $data = null ) : array | PDOStatement | string | Model 查找单条记录 如果不存在则抛出异常
force ( string $force ) 指定强制索引
getBind ( ) : array 获取绑定的参数 并清空
getConfig ( string $name = '' ) : boolean 获取数据库的配置参数
getConnection ( ) : Connection 获取当前的数据库Connection对象
getFieldsBind ( $options ) 获取当前数据表绑定信息
getFieldsType ( $options ) 获取当前数据表字段类型
getLastInsID ( string $sequence = null ) : string 获取最近插入的ID
getLastSql ( ) : string 获取最近一次查询的sql语句
getOptions ( string $name = '' ) : mixed 获取当前的查询参数
getPartitionTableName ( array $data, string $field, array $rule = [] ) : string 得到分表的的数据表名
getPk ( string | array $options = '' ) : string | array 获取当前数据表的主键
getTable ( string $name = '' ) : string 得到当前或者指定名称的数据表
getTableFields ( $options ) 获取当前数据表字段信息
getTableInfo ( mixed $tableName = '', string $fetch = '' ) : mixed 获取数据表信息
group ( string $group ) 指定group查询
having ( string $having ) 指定having查询
insert ( array $data, boolean $replace = false, boolean $getLastInsID = false, string $sequence = null ) : integer | string 插入记录
insertAll ( array $dataSet ) : integer | string 批量插入记录
insertGetId ( array $data, boolean $replace = false, string $sequence = null ) : integer | string 插入记录并获取自增ID
isBind ( string $key ) : boolean 检测参数是否已经绑定
join ( mixed $join, mixed $condition = null, string $type = 'INNER' ) 查询SQL组装 join
limit ( mixed $offset, mixed $length = null ) 指定查询数量
lock ( boolean $lock = false ) 指定查询lock
master ( ) 设置从主服务器读取数据
max ( string $field = '*' ) : float | integer MAX查询
min ( string $field = '*' ) : float | integer MIN查询
name ( string $name ) 指定默认的数据表名(不含前缀)
order ( string | array $field, string $order = null ) 指定排序 order('id','desc') 或者 order(['id'=>'desc','create_time'=>'desc'])
page ( mixed $page, mixed $listRows = null ) 指定分页
paginate ( integer | null $listRows = null, integer | boolean $simple = false, array $config = [] ) : Collection 分页查询
parseSqlTable ( string $sql ) : string 将SQL语句中的__TABLE_NAME__字符串替换成带前缀的表名(小写)
partition ( array $data, string $field, array $rule = [] ) 设置分表规则
pk ( string $pk ) 指定数据表主键
query ( string $sql, array $bind = [], boolean $master = false, boolean | string $class = false ) : mixed 执行查询 返回数据集
relation ( string $relation ) 设置关联查询
rollback ( ) : void 事务回滚
select ( array | string | Query | Closure $data = null ) : Collection | false | PDOStatement | string 查找记录
selectInsert ( string $fields, string $table ) : integer | string 通过Select方式插入记录
selectOrFail ( array | string | Query | Closure $data = null ) : array | PDOStatement | string | Model 查找多条记录 如果不存在则抛出异常
sequence ( string $sequence = null ) 设置自增序列名
setDec ( string $field, integer $step = 1, integer $lazyTime ) : integer | true 字段值(延迟)减少
setField ( string | array $field, mixed $value = '' ) : integer 设置记录的某个字段值 支持使用数据库字段和方法
setInc ( string $field, integer $step = 1, integer $lazyTime ) : integer | true 字段值(延迟)增长
setTable ( string $table ) 指定默认数据表名(含前缀)
startTrans ( ) : void 启动事务
strict ( boolean $strict = true ) 设置是否严格检查字段名
sum ( string $field = '*' ) : float | integer SUM查询
table ( mixed $table ) 指定当前操作的数据表
transaction ( callable $callback ) : mixed 执行数据库事务
union ( mixed $union, boolean $all = false ) 查询SQL组装 union
update ( array $data ) : integer | string 更新记录
using ( mixed $using ) USING支持 用于多表删除
value ( string $field, mixed $default = null ) : mixed 得到某个字段的值
via ( string $via = '' ) 设置当前字段添加的表别名
view ( $join, string | array $field = true, string | array $on = null, string $type = 'INNER' ) 指定JOIN查询字段
where ( mixed $field, mixed $op = null, mixed $condition = null ) 指定AND查询条件
whereOr ( mixed $field, mixed $op = null, mixed $condition = null ) 指定OR查询条件
whereTime ( string $field, string $op, string | array $range = null ) 查询日期或者时间
whereXor ( mixed $field, mixed $op = null, mixed $condition = null ) 指定XOR查询条件
with ( string | array $with ) 设置关联查询JOIN预查询
withField ( string | array $field ) 关联预加载中 获取关联指定字段值 example: Model::with(['relation' => function($query){ $query->withField("id,name"); }])

Protected Methods

Method Description
builder ( ) : Builder 获取当前的builder实例对象
getFieldBindType ( string $type ) : integer 获取字段绑定类型
lazyWrite ( string $type, string $guid, integer $step, integer $lazyTime ) : false | integer 延时更新检查 返回false表示需要延时 否则返回实际写入的数值
options ( array $options ) 查询参数赋值
parseExpress ( ) : array 分析表达式(可用于查询或者写入操作)
parsePkWhere ( array | string $data, mixed &$options ) : void 把主键值转换为查询条件 支持复合主键
parseWhereExp ( string $logic, string | array | Closure $field, mixed $op, mixed $condition, array $param = [] ) : void 分析查询表达式
throwNotFound ( array $options = [] ) 查询失败 抛出异常

Method Details

__call() public method

利用__call方法实现一些特殊的Model方法
public __call ( string $method, array $args ) : mixed
$method string 方法名称
$args array 调用参数
return mixed

__construct() public method

架构函数
public __construct ( Connection $connection = null, string $model = '' )
$connection Connection 数据库对象实例
$model string 模型名

alias() public method

指定数据表别名
public alias ( mixed $alias )
$alias mixed 数据表别名

avg() public method

AVG查询
public avg ( string $field = '*' ) : float | integer
$field string 字段名
return float | integer

batchQuery() public method

批处理执行SQL语句 批处理的指令都认为是execute操作
public batchQuery ( array $sql = [] ) : boolean
$sql array SQL批处理指令
return boolean

bind() public method

参数绑定
public bind ( mixed $key, mixed $value = false, integer $type = PDO::PARAM_STR )
$key mixed 参数名
$value mixed 绑定变量值
$type integer 绑定类型

buildSql() public method

创建子查询SQL
public buildSql ( boolean $sub = true ) : string
$sub boolean
return string

builder() protected method

获取当前的builder实例对象
protected builder ( ) : Builder
return Builder

cache() public method

查询缓存
public cache ( mixed $key = true, integer $expire = null, string $tag = null )
$key mixed 缓存key
$expire integer 缓存有效期
$tag string 缓存标签

chunk() public method

分批数据返回处理
public chunk ( integer $count, callable $callback, string $column = null ) : boolean
$count integer 每次处理的数据数量
$callback callable 处理回调方法
$column string 分批处理的字段名
return boolean

column() public method

得到某个列的数组
public column ( string $field, string $key = '' ) : array
$field string 字段名 多个字段用逗号分隔
$key string 索引
return array

comment() public method

查询注释
public comment ( string $comment )
$comment string 注释

commit() public method

用于非自动提交状态下面的查询提交
public commit ( ) : void
return void

connect() public method

切换当前的数据库连接
public connect ( mixed $config )
$config mixed

count() public method

COUNT查询
public count ( string $field = '*' ) : integer
$field string 字段名
return integer

delete() public method

删除记录
public delete ( mixed $data = null ) : integer
$data mixed 表达式 true 表示强制删除
return integer

distinct() public method

指定distinct查询
public distinct ( string $distinct )
$distinct string 是否唯一

execute() public method

执行语句
public execute ( string $sql, array $bind = [] ) : integer
$sql string sql指令
$bind array 参数绑定
return integer

failException() public method

设置查询数据不存在是否抛出异常
public failException ( boolean $fail = true )
$fail boolean 数据不存在是否抛出异常

fetchClass() public method

指定数据集返回对象
public fetchClass ( string $class )
$class string 指定返回的数据集对象类名

fetchPdo() public method

不主动获取数据集
public fetchPdo ( boolean $pdo = true )
$pdo boolean 是否返回 PDOStatement 对象

fetchSql() public method

获取执行的SQL语句
public fetchSql ( boolean $fetch = true )
$fetch boolean 是否返回sql

field() public method

指定查询字段 支持字段排除和指定数据表
public field ( mixed $field, boolean $except = false, string $tableName = '', string $prefix = '', string $alias = '' )
$field mixed
$except boolean 是否排除
$tableName string 数据表名
$prefix string 字段前缀
$alias string 别名前缀

find() public method

查找单条记录
public find ( array | string | Query | Closure $data = null ) : array | false | PDOStatement | string | Model
$data array | string | Query | Closure
return array | false | PDOStatement | string | think\Model

findOrFail() public method

查找单条记录 如果不存在则抛出异常
public findOrFail ( array | string | Query | Closure $data = null ) : array | PDOStatement | string | Model
$data array | string | Query | Closure
return array | PDOStatement | string | think\Model

force() public method

指定强制索引
public force ( string $force )
$force string 索引名称

getBind() public method

获取绑定的参数 并清空
public getBind ( ) : array
return array

getConfig() public method

获取数据库的配置参数
public getConfig ( string $name = '' ) : boolean
$name string 参数名称
return boolean

getConnection() public method

获取当前的数据库Connection对象
public getConnection ( ) : Connection
return Connection

getFieldBindType() protected method

获取字段绑定类型
protected getFieldBindType ( string $type ) : integer
$type string 字段类型
return integer

getFieldsBind() public method

获取当前数据表绑定信息
public getFieldsBind ( $options )

getFieldsType() public method

获取当前数据表字段类型
public getFieldsType ( $options )

getLastInsID() public method

获取最近插入的ID
public getLastInsID ( string $sequence = null ) : string
$sequence string 自增序列名
return string

getLastSql() public method

获取最近一次查询的sql语句
public getLastSql ( ) : string
return string

getOptions() public method

获取当前的查询参数
public getOptions ( string $name = '' ) : mixed
$name string 参数名
return mixed

getPartitionTableName() public method

得到分表的的数据表名
public getPartitionTableName ( array $data, string $field, array $rule = [] ) : string
$data array 操作的数据
$field string 分表依据的字段
$rule array 分表规则
return string

getPk() public method

获取当前数据表的主键
public getPk ( string | array $options = '' ) : string | array
$options string | array 数据表名或者查询参数
return string | array

getTable() public method

得到当前或者指定名称的数据表
public getTable ( string $name = '' ) : string
$name string
return string

getTableFields() public method

获取当前数据表字段信息
public getTableFields ( $options )

getTableInfo() public method

获取数据表信息
public getTableInfo ( mixed $tableName = '', string $fetch = '' ) : mixed
$tableName mixed 数据表名 留空自动获取
$fetch string 获取信息类型 包括 fields type bind pk
return mixed

group() public method

指定group查询
public group ( string $group )
$group string GROUP

having() public method

指定having查询
public having ( string $having )
$having string having

insert() public method

插入记录
public insert ( array $data, boolean $replace = false, boolean $getLastInsID = false, string $sequence = null ) : integer | string
$data array 数据
$replace boolean 是否replace
$getLastInsID boolean 返回自增主键
$sequence string 自增序列名
return integer | string

insertAll() public method

批量插入记录
public insertAll ( array $dataSet ) : integer | string
$dataSet array 数据集
return integer | string

insertGetId() public method

插入记录并获取自增ID
public insertGetId ( array $data, boolean $replace = false, string $sequence = null ) : integer | string
$data array 数据
$replace boolean 是否replace
$sequence string 自增序列名
return integer | string

isBind() public method

检测参数是否已经绑定
public isBind ( string $key ) : boolean
$key string 参数名
return boolean

join() public method

查询SQL组装 join
public join ( mixed $join, mixed $condition = null, string $type = 'INNER' )
$join mixed 关联的表名
$condition mixed 条件
$type string JOIN类型

lazyWrite() protected method

延时更新检查 返回false表示需要延时 否则返回实际写入的数值
protected lazyWrite ( string $type, string $guid, integer $step, integer $lazyTime ) : false | integer
$type string 自增或者自减
$guid string 写入标识
$step integer 写入步进值
$lazyTime integer 延时时间(s)
return false | integer

limit() public method

指定查询数量
public limit ( mixed $offset, mixed $length = null )
$offset mixed 起始位置
$length mixed 查询数量

lock() public method

指定查询lock
public lock ( boolean $lock = false )
$lock boolean 是否lock

master() public method

设置从主服务器读取数据
public master ( )

max() public method

MAX查询
public max ( string $field = '*' ) : float | integer
$field string 字段名
return float | integer

min() public method

MIN查询
public min ( string $field = '*' ) : float | integer
$field string 字段名
return float | integer

name() public method

指定默认的数据表名(不含前缀)
public name ( string $name )
$name string

options() protected method

查询参数赋值
protected options ( array $options )
$options array 表达式参数

order() public method

指定排序 order('id','desc') 或者 order(['id'=>'desc','create_time'=>'desc'])
public order ( string | array $field, string $order = null )
$field string | array 排序字段
$order string 排序

page() public method

指定分页
public page ( mixed $page, mixed $listRows = null )
$page mixed 页数
$listRows mixed 每页数量

paginate() public method

分页查询
public paginate ( integer | null $listRows = null, integer | boolean $simple = false, array $config = [] ) : Collection
$listRows integer | null 每页数量
$simple integer | boolean 简洁模式或者总记录数
$config array 配置参数 page:当前页, path:url路径, query:url额外参数, fragment:url锚点, var_page:分页变量, list_rows:每页数量 type:分页类名
return think\paginator\Collection

parseExpress() protected method

分析表达式(可用于查询或者写入操作)
protected parseExpress ( ) : array
return array

parsePkWhere() protected method

把主键值转换为查询条件 支持复合主键
protected parsePkWhere ( array | string $data, mixed &$options ) : void
$data array | string 主键数据
$options mixed 表达式参数
return void

parseSqlTable() public method

将SQL语句中的__TABLE_NAME__字符串替换成带前缀的表名(小写)
public parseSqlTable ( string $sql ) : string
$sql string sql语句
return string

parseWhereExp() protected method

分析查询表达式
protected parseWhereExp ( string $logic, string | array | Closure $field, mixed $op, mixed $condition, array $param = [] ) : void
$logic string 查询逻辑 and or xor
$field string | array | Closure 查询字段
$op mixed 查询表达式
$condition mixed 查询条件
$param array 查询参数
return void

partition() public method

设置分表规则
public partition ( array $data, string $field, array $rule = [] )
$data array 操作的数据
$field string 分表依据的字段
$rule array 分表规则

pk() public method

指定数据表主键
public pk ( string $pk )
$pk string 主键

query() public method

执行查询 返回数据集
public query ( string $sql, array $bind = [], boolean $master = false, boolean | string $class = false ) : mixed
$sql string sql指令
$bind array 参数绑定
$master boolean 是否在主服务器读操作
$class boolean | string 指定返回的数据集对象
return mixed

relation() public method

设置关联查询
public relation ( string $relation )
$relation string 关联名称

rollback() public method

事务回滚
public rollback ( ) : void
return void

select() public method

查找记录
public select ( array | string | Query | Closure $data = null ) : Collection | false | PDOStatement | string
$data array | string | Query | Closure
return think\Collection | false | PDOStatement | string

selectInsert() public method

通过Select方式插入记录
public selectInsert ( string $fields, string $table ) : integer | string
$fields string 要插入的数据表字段名
$table string 要插入的数据表名
return integer | string

selectOrFail() public method

查找多条记录 如果不存在则抛出异常
public selectOrFail ( array | string | Query | Closure $data = null ) : array | PDOStatement | string | Model
$data array | string | Query | Closure
return array | PDOStatement | string | think\Model

sequence() public method

设置自增序列名
public sequence ( string $sequence = null )
$sequence string 自增序列名

setDec() public method

字段值(延迟)减少
public setDec ( string $field, integer $step = 1, integer $lazyTime ) : integer | true
$field string 字段名
$step integer 减少值
$lazyTime integer 延时时间(s)
return integer | true

setField() public method

设置记录的某个字段值 支持使用数据库字段和方法
public setField ( string | array $field, mixed $value = '' ) : integer
$field string | array 字段名
$value mixed 字段值
return integer

setInc() public method

字段值(延迟)增长
public setInc ( string $field, integer $step = 1, integer $lazyTime ) : integer | true
$field string 字段名
$step integer 增长值
$lazyTime integer 延时时间(s)
return integer | true

setTable() public method

指定默认数据表名(含前缀)
public setTable ( string $table )
$table string 表名

startTrans() public method

启动事务
public startTrans ( ) : void
return void

strict() public method

设置是否严格检查字段名
public strict ( boolean $strict = true )
$strict boolean 是否严格检查字段

sum() public method

SUM查询
public sum ( string $field = '*' ) : float | integer
$field string 字段名
return float | integer

table() public method

指定当前操作的数据表
public table ( mixed $table )
$table mixed 表名

throwNotFound() protected method

查询失败 抛出异常
protected throwNotFound ( array $options = [] )
$options array 查询参数

transaction() public method

执行数据库事务
public transaction ( callable $callback ) : mixed
$callback callable 数据操作方法回调
return mixed

union() public method

查询SQL组装 union
public union ( mixed $union, boolean $all = false )
$union mixed
$all boolean

update() public method

更新记录
public update ( array $data ) : integer | string
$data array 数据
return integer | string

using() public method

USING支持 用于多表删除
public using ( mixed $using )
$using mixed

value() public method

得到某个字段的值
public value ( string $field, mixed $default = null ) : mixed
$field string 字段名
$default mixed 默认值
return mixed

via() public method

设置当前字段添加的表别名
public via ( string $via = '' )
$via string

view() public method

指定JOIN查询字段
public view ( $join, string | array $field = true, string | array $on = null, string $type = 'INNER' )
$field string | array 查询字段
$on string | array JOIN条件
$type string JOIN类型

where() public method

指定AND查询条件
public where ( mixed $field, mixed $op = null, mixed $condition = null )
$field mixed 查询字段
$op mixed 查询表达式
$condition mixed 查询条件

whereOr() public method

指定OR查询条件
public whereOr ( mixed $field, mixed $op = null, mixed $condition = null )
$field mixed 查询字段
$op mixed 查询表达式
$condition mixed 查询条件

whereTime() public method

查询日期或者时间
public whereTime ( string $field, string $op, string | array $range = null )
$field string 日期字段名
$op string 比较运算符或者表达式
$range string | array 比较范围

whereXor() public method

指定XOR查询条件
public whereXor ( mixed $field, mixed $op = null, mixed $condition = null )
$field mixed 查询字段
$op mixed 查询表达式
$condition mixed 查询条件

with() public method

设置关联查询JOIN预查询
public with ( string | array $with )
$with string | array 关联方法名称

withField() public method

关联预加载中 获取关联指定字段值 example: Model::with(['relation' => function($query){ $query->withField("id,name"); }])
public withField ( string | array $field )
$field string | array

Property Details

$bind protected property

参数绑定
protected $bind

$builder protected property

数据库驱动类型
protected $builder

$connection protected property

数据库Connection对象实例
protected $connection

$info protected static property

数据表信息
protected static $info

$model protected property

当前模型类名称
protected $model

$name protected property

当前数据表名称(不含前缀)
protected $name

$options protected property

查询参数
protected $options

$pk protected property

当前数据表主键
protected $pk

$prefix protected property

当前数据表前缀
protected $prefix

$table protected property

当前数据表名称(含前缀)
protected $table