\think\dbQuery

Summary

Methods
Properties
Constants
__construct()
newQuery()
__call()
extend()
setConnection()
getConnection()
model()
getModel()
readMaster()
name()
getName()
getTable()
query()
execute()
listen()
getLastInsID()
getNumRows()
getLastSql()
transactionXa()
transaction()
startTrans()
commit()
rollback()
batchQuery()
getConfig()
getTableFields()
getFieldsType()
getPartitionTableName()
value()
column()
aggregate()
count()
sum()
min()
max()
avg()
setField()
setInc()
setDec()
join()
leftJoin()
rightJoin()
fullJoin()
union()
unionAll()
field()
fieldRaw()
data()
inc()
dec()
exp()
raw()
view()
partition()
where()
whereOr()
whereXor()
whereNull()
whereNotNull()
whereExists()
whereNotExists()
whereIn()
whereNotIn()
whereLike()
whereNotLike()
whereBetween()
whereNotBetween()
whereColumn()
useSoftDelete()
whereExp()
whereRaw()
whereOrRaw()
removeWhereField()
removeOption()
when()
limit()
page()
paginate()
table()
using()
order()
orderRaw()
orderField()
orderRand()
cache()
group()
having()
lock()
distinct()
alias()
force()
comment()
fetchSql()
fetchPdo()
fetchCollection()
master()
strict()
failException()
sequence()
hidden()
visible()
append()
withAttr()
json()
setJsonFieldType()
getJsonFieldType()
allowEmpty()
scope()
withSearch()
pk()
timeRule()
whereTime()
whereBetweenTimeField()
whereNotBetweenTimeField()
whereBetweenTime()
getPk()
bind()
isBind()
option()
getOptions()
setOption()
with()
withJoin()
withCount()
withSum()
withMax()
withMin()
withAvg()
withField()
via()
relation()
insert()
insertGetId()
insertAll()
selectInsert()
update()
delete()
getPdo()
cursor()
select()
find()
get()
getOrFail()
all()
selectOrFail()
findOrFail()
findOrEmpty()
chunk()
getBind()
buildSql()
parsePkWhere()
event()
trigger()
No public properties found
No constants found
lazyWrite()
getJoinTable()
bindParams()
parseWhereExp()
parseWhereItem()
parseArrayWhereItems()
options()
withAggregate()
resultSetToModelCollection()
resultSet()
resultToEmpty()
parseQuery()
result()
getResultAttr()
jsonResult()
resultToModel()
getModelUpdateCondition()
throwNotFound()
parseView()
parseOptions()
$connection
$model
$name
$pk
$prefix
$options
$bind
$readMaster
$timeRule
$timeExp
N/A
No private methods found
$event
$extend
N/A

Properties

$connection

$connection : \think\db\Connection

当前数据库连接对象

Type

Connection

$model

$model : \think\Model

当前模型对象

Type

Model

$name

$name : string

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

Type

string

$pk

$pk : string|array

当前数据表主键

Type

string|array

$prefix

$prefix : string

当前数据表前缀

Type

string

$options

$options : array

当前查询参数

Type

array

$bind

$bind : array

当前参数绑定

Type

array

$readMaster

$readMaster : array

读取主库的表

Type

array

$timeRule

$timeRule : array

日期查询表达式

Type

array

$timeExp

$timeExp : array

日期查询快捷定义

Type

array

$event

$event : array

事件回调

Type

array

$extend

$extend : array

扩展查询方法

Type

array

Methods

__construct()

__construct(\think\db\Connection  $connection = null) : mixed

架构函数

Parameters

\think\db\Connection $connection

Returns

mixed —

newQuery()

newQuery() : \think\db\Query

创建一个新的查询对象

Returns

\think\db\Query —

__call()

__call(string  $method, array  $args) : mixed

利用__call方法实现一些特殊的Model方法

Parameters

string $method

方法名称

array $args

调用参数

Throws

\think\exception\DbException
\think\Exception

Returns

mixed —

extend()

extend(string|array  $method, callable  $callback = null) : void

扩展查询方法

Parameters

string|array $method

查询方法名

callable $callback

setConnection()

setConnection(\think\db\Connection  $connection) : $this

设置当前的数据库Connection对象

Parameters

\think\db\Connection $connection

Returns

$this —

getConnection()

getConnection() : \think\db\Connection

获取当前的数据库Connection对象

Returns

\think\db\Connection —

model()

model(\think\Model  $model) : $this

指定模型

Parameters

\think\Model $model

模型对象实例

Returns

$this —

getModel()

getModel() : \think\Model|null

获取当前的模型对象

Returns

\think\Model|null —

readMaster()

readMaster(bool  $all = false) : $this

设置从主库读取数据

Parameters

bool $all

是否所有表有效

Returns

$this —

name()

name(string  $name) : $this

指定当前数据表名(不含前缀)

Parameters

string $name

Returns

$this —

getName()

getName() : string

获取当前的数据表名称

Returns

string —

getTable()

getTable(string  $name = '') : string

得到当前或者指定名称的数据表

Parameters

string $name

Returns

string —

query()

query(string  $sql, array  $bind = [], bool  $master = false, bool  $pdo = false) : mixed

执行查询 返回数据集

Parameters

string $sql

sql指令

array $bind

参数绑定

bool $master

是否在主服务器读操作

bool $pdo

是否返回PDO对象

Throws

\think\db\exception\BindParamException
\think\exception\PDOException

Returns

mixed —

execute()

execute(string  $sql, array  $bind = []) : int

执行语句

Parameters

string $sql

sql指令

array $bind

参数绑定

Throws

\think\db\exception\BindParamException
\think\exception\PDOException

Returns

int —

listen()

listen(callable  $callback) : void

监听SQL执行

Parameters

callable $callback

回调方法

getLastInsID()

getLastInsID(string  $sequence = null) : string

获取最近插入的ID

Parameters

string $sequence

自增序列名

Returns

string —

getNumRows()

getNumRows() : int

获取返回或者影响的记录数

Returns

int —

getLastSql()

getLastSql() : string

获取最近一次查询的sql语句

Returns

string —

transactionXa()

transactionXa(callable  $callback, array  $dbs = []) : mixed

执行数据库Xa事务

Parameters

callable $callback

数据操作方法回调

array $dbs

多个查询对象或者连接对象

Throws

\think\exception\PDOException
\Exception
\Throwable

Returns

mixed —

transaction()

transaction(callable  $callback) : mixed

执行数据库事务

Parameters

callable $callback

数据操作方法回调

Returns

mixed —

startTrans()

startTrans() : void

启动事务

commit()

commit() : void

用于非自动提交状态下面的查询提交

Throws

\think\exception\PDOException

rollback()

rollback() : void

事务回滚

Throws

\think\exception\PDOException

batchQuery()

batchQuery(array  $sql = []) : bool

批处理执行SQL语句 批处理的指令都认为是execute操作

Parameters

array $sql

SQL批处理指令

Returns

bool —

getConfig()

getConfig(string  $name = '') : mixed

获取数据库的配置参数

Parameters

string $name

参数名称

Returns

mixed —

getTableFields()

getTableFields(string  $tableName = '') : array

获取数据表字段信息

Parameters

string $tableName

数据表名

Returns

array —

getFieldsType()

getFieldsType(string  $tableName = '', string  $field = null) : array|string

获取数据表字段类型

Parameters

string $tableName

数据表名

string $field

字段名

Returns

array|string —

getPartitionTableName()

getPartitionTableName(array  $data, string  $field, array  $rule = []) : array

得到分表的的数据表名

Parameters

array $data

操作的数据

string $field

分表依据的字段

array $rule

分表规则

Returns

array —

value()

value(string  $field, mixed  $default = null) : mixed

得到某个字段的值

Parameters

string $field

字段名

mixed $default

默认值

Returns

mixed —

column()

column(string  $field, string  $key = '') : array

得到某个列的数组

Parameters

string $field

字段名 多个字段用逗号分隔

string $key

索引

Returns

array —

aggregate()

aggregate(string  $aggregate, string|\think\db\Expression  $field, bool  $force = false) : mixed

聚合查询

Parameters

string $aggregate

聚合方法

string|\think\db\Expression $field

字段名

bool $force

强制转为数字类型

Returns

mixed —

count()

count(string|\think\db\Expression  $field = '*') : float|string

COUNT查询

Parameters

string|\think\db\Expression $field

字段名

Returns

float|string —

sum()

sum(string|\think\db\Expression  $field) : float

SUM查询

Parameters

string|\think\db\Expression $field

字段名

Returns

float —

min()

min(string|\think\db\Expression  $field, bool  $force = true) : mixed

MIN查询

Parameters

string|\think\db\Expression $field

字段名

bool $force

强制转为数字类型

Returns

mixed —

max()

max(string|\think\db\Expression  $field, bool  $force = true) : mixed

MAX查询

Parameters

string|\think\db\Expression $field

字段名

bool $force

强制转为数字类型

Returns

mixed —

avg()

avg(string|\think\db\Expression  $field) : float

AVG查询

Parameters

string|\think\db\Expression $field

字段名

Returns

float —

setField()

setField(string|array  $field, mixed  $value = '') : int

设置记录的某个字段值 支持使用数据库字段和方法

Parameters

string|array $field

字段名

mixed $value

字段值

Returns

int —

setInc()

setInc(string  $field, int  $step = 1, int  $lazyTime) : int|true

字段值(延迟)增长

Parameters

string $field

字段名

int $step

增长值

int $lazyTime

延时时间(s)

Throws

\think\Exception

Returns

int|true —

setDec()

setDec(string  $field, int  $step = 1, int  $lazyTime) : int|true

字段值(延迟)减少

Parameters

string $field

字段名

int $step

减少值

int $lazyTime

延时时间(s)

Throws

\think\Exception

Returns

int|true —

join()

join(mixed  $join, mixed  $condition = null, string  $type = 'INNER', array  $bind = []) : $this

查询SQL组装 join

Parameters

mixed $join

关联的表名

mixed $condition

条件

string $type

JOIN类型

array $bind

参数绑定

Returns

$this —

leftJoin()

leftJoin(mixed  $join, mixed  $condition = null, array  $bind = []) : $this

LEFT JOIN

Parameters

mixed $join

关联的表名

mixed $condition

条件

array $bind

参数绑定

Returns

$this —

rightJoin()

rightJoin(mixed  $join, mixed  $condition = null, array  $bind = []) : $this

RIGHT JOIN

Parameters

mixed $join

关联的表名

mixed $condition

条件

array $bind

参数绑定

Returns

$this —

fullJoin()

fullJoin(mixed  $join, mixed  $condition = null, array  $bind = []) : $this

FULL JOIN

Parameters

mixed $join

关联的表名

mixed $condition

条件

array $bind

参数绑定

Returns

$this —

union()

union(mixed  $union, bool  $all = false) : $this

查询SQL组装 union

Parameters

mixed $union
bool $all

Returns

$this —

unionAll()

unionAll(mixed  $union) : $this

查询SQL组装 union all

Parameters

mixed $union

Returns

$this —

field()

field(mixed  $field, bool  $except = false, string  $tableName = '', string  $prefix = '', string  $alias = '') : $this

指定查询字段 支持字段排除和指定数据表

Parameters

mixed $field
bool $except

是否排除

string $tableName

数据表名

string $prefix

字段前缀

string $alias

别名前缀

Returns

$this —

fieldRaw()

fieldRaw(string  $field) : $this

表达式方式指定查询字段

Parameters

string $field

字段名

Returns

$this —

data()

data(mixed  $field, mixed  $value = null) : $this

设置数据

Parameters

mixed $field

字段名或者数据

mixed $value

字段值

Returns

$this —

inc()

inc(string|array  $field, int  $step = 1, mixed  $op = 'INC') : $this

字段值增长

Parameters

string|array $field

字段名

int $step

增长值

mixed $op

Returns

$this —

dec()

dec(string|array  $field, int  $step = 1) : $this

字段值减少

Parameters

string|array $field

字段名

int $step

增长值

Returns

$this —

exp()

exp(string  $field, string  $value) : $this

使用表达式设置数据

Parameters

string $field

字段名

string $value

字段值

Returns

$this —

raw()

raw(mixed  $value) : \think\db\Expression

使用表达式设置数据

Parameters

mixed $value

表达式

Returns

\think\db\Expression —

view()

view(mixed  $join, string|array  $field = true, mixed  $on = null, string  $type = 'INNER') : $this

指定JOIN查询字段

Parameters

mixed $join
string|array $field

查询字段

mixed $on

JOIN条件

string $type

JOIN类型

Returns

$this —

partition()

partition(array  $data, string  $field, array  $rule = []) : $this

设置分表规则

Parameters

array $data

操作的数据

string $field

分表依据的字段

array $rule

分表规则

Returns

$this —

where()

where(mixed  $field, mixed  $op = null, mixed  $condition = null) : $this

指定AND查询条件

Parameters

mixed $field

查询字段

mixed $op

查询表达式

mixed $condition

查询条件

Returns

$this —

whereOr()

whereOr(mixed  $field, mixed  $op = null, mixed  $condition = null) : $this

指定OR查询条件

Parameters

mixed $field

查询字段

mixed $op

查询表达式

mixed $condition

查询条件

Returns

$this —

whereXor()

whereXor(mixed  $field, mixed  $op = null, mixed  $condition = null) : $this

指定XOR查询条件

Parameters

mixed $field

查询字段

mixed $op

查询表达式

mixed $condition

查询条件

Returns

$this —

whereNull()

whereNull(mixed  $field, string  $logic = 'AND') : $this

指定Null查询条件

Parameters

mixed $field

查询字段

string $logic

查询逻辑 and or xor

Returns

$this —

whereNotNull()

whereNotNull(mixed  $field, string  $logic = 'AND') : $this

指定NotNull查询条件

Parameters

mixed $field

查询字段

string $logic

查询逻辑 and or xor

Returns

$this —

whereExists()

whereExists(mixed  $condition, string  $logic = 'AND') : $this

指定Exists查询条件

Parameters

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this —

whereNotExists()

whereNotExists(mixed  $condition, string  $logic = 'AND') : $this

指定NotExists查询条件

Parameters

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this —

whereIn()

whereIn(mixed  $field, mixed  $condition, string  $logic = 'AND') : $this

指定In查询条件

Parameters

mixed $field

查询字段

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this —

whereNotIn()

whereNotIn(mixed  $field, mixed  $condition, string  $logic = 'AND') : $this

指定NotIn查询条件

Parameters

mixed $field

查询字段

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this —

whereLike()

whereLike(mixed  $field, mixed  $condition, string  $logic = 'AND') : $this

指定Like查询条件

Parameters

mixed $field

查询字段

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this —

whereNotLike()

whereNotLike(mixed  $field, mixed  $condition, string  $logic = 'AND') : $this

指定NotLike查询条件

Parameters

mixed $field

查询字段

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this —

whereBetween()

whereBetween(mixed  $field, mixed  $condition, string  $logic = 'AND') : $this

指定Between查询条件

Parameters

mixed $field

查询字段

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this —

whereNotBetween()

whereNotBetween(mixed  $field, mixed  $condition, string  $logic = 'AND') : $this

指定NotBetween查询条件

Parameters

mixed $field

查询字段

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this —

whereColumn()

whereColumn(string|array  $field1, string  $operator = null, string  $field2 = null, string  $logic = 'AND') : $this

比较两个字段

Parameters

string|array $field1

查询字段

string $operator

比较操作符

string $field2

比较字段

string $logic

查询逻辑 and or xor

Returns

$this —

useSoftDelete()

useSoftDelete(false|string  $field, mixed  $condition = null) : $this

设置软删除字段及条件

Parameters

false|string $field

查询字段

mixed $condition

查询条件

Returns

$this —

whereExp()

whereExp(mixed  $field, string  $where, array  $bind = [], string  $logic = 'AND') : $this

指定Exp查询条件

Parameters

mixed $field

查询字段

string $where

查询条件

array $bind

参数绑定

string $logic

查询逻辑 and or xor

Returns

$this —

whereRaw()

whereRaw(string  $where, array  $bind = [], string  $logic = 'AND') : $this

指定表达式查询条件

Parameters

string $where

查询条件

array $bind

参数绑定

string $logic

查询逻辑 and or xor

Returns

$this —

whereOrRaw()

whereOrRaw(string  $where, array  $bind = []) : $this

指定表达式查询条件 OR

Parameters

string $where

查询条件

array $bind

参数绑定

Returns

$this —

removeWhereField()

removeWhereField(string  $field, string  $logic = 'AND') : $this

去除某个查询条件

Parameters

string $field

查询字段

string $logic

查询逻辑 and or xor

Returns

$this —

removeOption()

removeOption(string|bool  $option = true) : $this

去除查询参数

Parameters

string|bool $option

参数名 true 表示去除所有参数

Returns

$this —

when()

when(mixed  $condition, \Closure|array  $query, \Closure|array  $otherwise = null) : $this

条件查询

Parameters

mixed $condition

满足条件(支持闭包)

\Closure|array $query

满足条件后执行的查询表达式(闭包或数组)

\Closure|array $otherwise

不满足条件后执行

Returns

$this —

limit()

limit(mixed  $offset, mixed  $length = null) : $this

指定查询数量

Parameters

mixed $offset

起始位置

mixed $length

查询数量

Returns

$this —

page()

page(mixed  $page, mixed  $listRows = null) : $this

指定分页

Parameters

mixed $page

页数

mixed $listRows

每页数量

Returns

$this —

paginate()

paginate(int|array  $listRows = null, int|bool  $simple = false, array  $config = []) : $this[]|\think\Paginator

分页查询

Parameters

int|array $listRows

每页数量 数组表示配置参数

int|bool $simple

是否简洁模式或者总记录数

array $config

配置参数 page:当前页, path:url路径, query:url额外参数, fragment:url锚点, var_page:分页变量, list_rows:每页数量 type:分页类名

Throws

\think\exception\DbException

Returns

$this[]|\think\Paginator —

table()

table(mixed  $table) : $this

指定当前操作的数据表

Parameters

mixed $table

表名

Returns

$this —

using()

using(mixed  $using) : $this

USING支持 用于多表删除

Parameters

mixed $using

Returns

$this —

order()

order(string|array  $field, string  $order = null) : $this

指定排序 order('id','desc') 或者 order(['id'=>'desc','create_time'=>'desc'])

Parameters

string|array $field

排序字段

string $order

排序

Returns

$this —

orderRaw()

orderRaw(string  $field, array  $bind = []) : $this

表达式方式指定Field排序

Parameters

string $field

排序字段

array $bind

参数绑定

Returns

$this —

orderField()

orderField(string|array  $field, array  $values, string  $order = '') : $this

指定Field排序 order('id',[1,2,3],'desc')

Parameters

string|array $field

排序字段

array $values

排序值

string $order

Returns

$this —

orderRand()

orderRand() : $this

随机排序

Returns

$this —

cache()

cache(mixed  $key = true, int|\DateTime  $expire = null, string  $tag = null) : $this

查询缓存

Parameters

mixed $key

缓存key

int|\DateTime $expire

缓存有效期

string $tag

缓存标签

Returns

$this —

group()

group(string|array  $group) : $this

指定group查询

Parameters

string|array $group

GROUP

Returns

$this —

having()

having(string  $having) : $this

指定having查询

Parameters

string $having

having

Returns

$this —

lock()

lock(bool|string  $lock = false) : $this

指定查询lock

Parameters

bool|string $lock

是否lock

Returns

$this —

distinct()

distinct(string  $distinct) : $this

指定distinct查询

Parameters

string $distinct

是否唯一

Returns

$this —

alias()

alias(array|string  $alias) : $this

指定数据表别名

Parameters

array|string $alias

数据表别名

Returns

$this —

force()

force(string  $force) : $this

指定强制索引

Parameters

string $force

索引名称

Returns

$this —

comment()

comment(string  $comment) : $this

查询注释

Parameters

string $comment

注释

Returns

$this —

fetchSql()

fetchSql(bool  $fetch = true) : $this

获取执行的SQL语句

Parameters

bool $fetch

是否返回sql

Returns

$this —

fetchPdo()

fetchPdo(bool  $pdo = true) : $this

不主动获取数据集

Parameters

bool $pdo

是否返回 PDOStatement 对象

Returns

$this —

fetchCollection()

fetchCollection(bool|string  $collection = true) : $this

设置是否返回数据集对象(支持设置数据集对象类名)

Parameters

bool|string $collection

是否返回数据集对象

Returns

$this —

master()

master() : $this

设置从主服务器读取数据

Returns

$this —

strict()

strict(bool  $strict = true) : $this

设置是否严格检查字段名

Parameters

bool $strict

是否严格检查字段

Returns

$this —

failException()

failException(bool  $fail = true) : $this

设置查询数据不存在是否抛出异常

Parameters

bool $fail

数据不存在是否抛出异常

Returns

$this —

sequence()

sequence(string  $sequence = null) : $this

设置自增序列名

Parameters

string $sequence

自增序列名

Returns

$this —

hidden()

hidden(mixed  $hidden) : $this

设置需要隐藏的输出属性

Parameters

mixed $hidden

需要隐藏的字段名

Returns

$this —

visible()

visible(array  $visible) : $this

设置需要输出的属性

Parameters

array $visible

需要输出的属性

Returns

$this —

append()

append(array  $append = []) : $this

设置需要附加的输出属性

Parameters

array $append

属性列表

Returns

$this —

withAttr()

withAttr(string|array  $name, callable  $callback = null) : $this

设置数据字段获取器

Parameters

string|array $name

字段名

callable $callback

闭包获取器

Returns

$this —

json()

json(array  $json = [], bool  $assoc = false) : $this

设置JSON字段信息

Parameters

array $json

JSON字段

bool $assoc

是否取出数组

Returns

$this —

setJsonFieldType()

setJsonFieldType(array  $type) : $this

设置字段类型信息

Parameters

array $type

字段类型信息

Returns

$this —

getJsonFieldType()

getJsonFieldType(string  $field) : string|null

获取字段类型信息

Parameters

string $field

字段名

Returns

string|null —

allowEmpty()

allowEmpty(bool  $allowEmpty = true) : $this

是否允许返回空数据(或空模型)

Parameters

bool $allowEmpty

是否允许为空

Returns

$this —

scope()

scope(array|string|\Closure  $scope, array  ...$args) : $this

添加查询范围

Parameters

array|string|\Closure $scope

查询范围定义

array $args variadic

参数

Returns

$this —

withSearch()

withSearch(array  $fields, array  $data = [], string  $prefix = '') : $this

使用搜索器条件搜索字段

Parameters

array $fields

搜索字段

array $data

搜索数据

string $prefix

字段前缀标识

Returns

$this —

pk()

pk(string  $pk) : $this

指定数据表主键

Parameters

string $pk

主键

Returns

$this —

timeRule()

timeRule(string  $name, string|array  $rule) : $this

查询日期或者时间

Parameters

string $name

时间表达式

string|array $rule

时间范围

Returns

$this —

whereTime()

whereTime(string  $field, string|array  $op, string|array  $range = null, string  $logic = 'AND') : $this

查询日期或者时间

Parameters

string $field

日期字段名

string|array $op

比较运算符或者表达式

string|array $range

比较范围

string $logic

AND OR

Returns

$this —

whereBetweenTimeField()

whereBetweenTimeField(string  $startField, string  $endField) : $this

查询当前时间在两个时间字段范围

Parameters

string $startField

开始时间字段

string $endField

结束时间字段

Returns

$this —

whereNotBetweenTimeField()

whereNotBetweenTimeField(string  $startField, string  $endField) : $this

查询当前时间不在两个时间字段范围

Parameters

string $startField

开始时间字段

string $endField

结束时间字段

Returns

$this —

whereBetweenTime()

whereBetweenTime(string  $field, string  $startTime, string  $endTime = null, string  $logic = 'AND') : $this

查询日期或者时间范围

Parameters

string $field

日期字段名

string $startTime

开始时间

string $endTime

结束时间

string $logic

AND OR

Returns

$this —

getPk()

getPk(string|array  $options = '') : string|array

获取当前数据表的主键

Parameters

string|array $options

数据表名或者查询参数

Returns

string|array —

bind()

bind(mixed  $value, int  $type = PDO::PARAM_STR, string  $name = null) : $this|string

参数绑定

Parameters

mixed $value

绑定变量值

int $type

绑定类型

string $name

绑定名称

Returns

$this|string —

isBind()

isBind(string  $key) : bool

检测参数是否已经绑定

Parameters

string $key

参数名

Returns

bool —

option()

option(string  $name, mixed  $value) : $this

查询参数赋值

Parameters

string $name

参数名

mixed $value

Returns

$this —

getOptions()

getOptions(string  $name = '') : mixed

获取当前的查询参数

Parameters

string $name

参数名

Returns

mixed —

setOption()

setOption(string  $option, mixed  $value) : $this

设置当前的查询参数

Parameters

string $option

参数名

mixed $value

参数值

Returns

$this —

with()

with(string|array  $with) : $this

设置关联查询JOIN预查询

Parameters

string|array $with

关联方法名称

Returns

$this —

withJoin()

withJoin(string|array  $with, string  $joinType = '') : $this

关联预载入 JOIN方式(不支持嵌套)

Parameters

string|array $with

关联方法名

string $joinType

JOIN方式

Returns

$this —

withCount()

withCount(string|array  $relation, bool  $subQuery = true) : $this

关联统计

Parameters

string|array $relation

关联方法名

bool $subQuery

是否使用子查询

Returns

$this —

withSum()

withSum(string|array  $relation, string  $field, bool  $subQuery = true) : $this

关联统计Sum

Parameters

string|array $relation

关联方法名

string $field

字段

bool $subQuery

是否使用子查询

Returns

$this —

withMax()

withMax(string|array  $relation, string  $field, bool  $subQuery = true) : $this

关联统计Max

Parameters

string|array $relation

关联方法名

string $field

字段

bool $subQuery

是否使用子查询

Returns

$this —

withMin()

withMin(string|array  $relation, string  $field, bool  $subQuery = true) : $this

关联统计Min

Parameters

string|array $relation

关联方法名

string $field

字段

bool $subQuery

是否使用子查询

Returns

$this —

withAvg()

withAvg(string|array  $relation, string  $field, bool  $subQuery = true) : $this

关联统计Avg

Parameters

string|array $relation

关联方法名

string $field

字段

bool $subQuery

是否使用子查询

Returns

$this —

withField()

withField(mixed  $field) : $this

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

Parameters

mixed $field

Returns

$this —

via()

via(string  $via = '') : $this

设置当前字段添加的表别名

Parameters

string $via

Returns

$this —

relation()

relation(string|array  $relation) : $this

设置关联查询

Parameters

string|array $relation

关联名称

Returns

$this —

insert()

insert(array  $data = [], bool  $replace = false, bool  $getLastInsID = false, string  $sequence = null) : int|string

插入记录

Parameters

array $data

数据

bool $replace

是否replace

bool $getLastInsID

返回自增主键

string $sequence

自增序列名

Returns

int|string —

insertGetId()

insertGetId(array  $data, bool  $replace = false, string  $sequence = null) : int|string

插入记录并获取自增ID

Parameters

array $data

数据

bool $replace

是否replace

string $sequence

自增序列名

Returns

int|string —

insertAll()

insertAll(array  $dataSet = [], bool  $replace = false, int  $limit = null) : int|string

批量插入记录

Parameters

array $dataSet

数据集

bool $replace

是否replace

int $limit

每次写入数据限制

Returns

int|string —

selectInsert()

selectInsert(string  $fields, string  $table) : int|string

通过Select方式插入记录

Parameters

string $fields

要插入的数据表字段名

string $table

要插入的数据表名

Throws

\think\exception\PDOException

Returns

int|string —

update()

update(mixed  $data = []) : int|string

更新记录

Parameters

mixed $data

数据

Throws

\think\Exception
\think\exception\PDOException

Returns

int|string —

delete()

delete(mixed  $data = null) : int

删除记录

Parameters

mixed $data

表达式 true 表示强制删除

Throws

\think\Exception
\think\exception\PDOException

Returns

int —

getPdo()

getPdo() : \PDOStatement|string

执行查询但只返回PDOStatement对象

Returns

\PDOStatement|string —

cursor()

cursor(array|string|\think\db\Query|\Closure  $data = null) : \Generator

使用游标查找记录

Parameters

array|string|\think\db\Query|\Closure $data

Returns

\Generator —

select()

select(array|string|\think\db\Query|\Closure  $data = null) : \think\Collection|array|\PDOStatement|string

查找记录

Parameters

array|string|\think\db\Query|\Closure $data

Throws

\think\exception\DbException
\think\db\exception\ModelNotFoundException
\think\db\exception\DataNotFoundException

Returns

\think\Collection|array|\PDOStatement|string —

find()

find(array|string|\think\db\Query|\Closure  $data = null) : array|null|\PDOStatement|string|\think\Model

查找单条记录

Parameters

array|string|\think\db\Query|\Closure $data

Throws

\think\exception\DbException
\think\db\exception\ModelNotFoundException
\think\db\exception\DataNotFoundException

Returns

array|null|\PDOStatement|string|\think\Model —

get()

get(mixed  $data, mixed  $with = [], bool  $cache = false, bool  $failException = false) : static|null

查找单条记录

Parameters

mixed $data

主键值或者查询条件(闭包)

mixed $with

关联预查询

bool $cache

是否缓存

bool $failException

是否抛出异常

Throws

\think\db\exception\DbException

Returns

static|null —

getOrFail()

getOrFail(mixed  $data, mixed  $with = [], bool  $cache = false) : static|null

查找单条记录 如果不存在直接抛出异常

Parameters

mixed $data

主键值或者查询条件(闭包)

mixed $with

关联预查询

bool $cache

是否缓存

Throws

\think\db\exception\DbException

Returns

static|null —

all()

all(mixed  $data = null, array|string  $with = [], bool  $cache = false) : static[]|false

查找所有记录

Parameters

mixed $data

主键列表或者查询条件(闭包)

array|string $with

关联预查询

bool $cache

是否缓存

Throws

\think\db\exception\DbException

Returns

static[]|false —

selectOrFail()

selectOrFail(array|string|\think\db\Query|\Closure  $data = null) : array|\PDOStatement|string|\think\Model

查找多条记录 如果不存在则抛出异常

Parameters

array|string|\think\db\Query|\Closure $data

Throws

\think\exception\DbException
\think\db\exception\ModelNotFoundException
\think\db\exception\DataNotFoundException

Returns

array|\PDOStatement|string|\think\Model —

findOrFail()

findOrFail(array|string|\think\db\Query|\Closure  $data = null) : array|\PDOStatement|string|\think\Model

查找单条记录 如果不存在则抛出异常

Parameters

array|string|\think\db\Query|\Closure $data

Throws

\think\exception\DbException
\think\db\exception\ModelNotFoundException
\think\db\exception\DataNotFoundException

Returns

array|\PDOStatement|string|\think\Model —

findOrEmpty()

findOrEmpty(array|string|\think\db\Query|\Closure  $data = null) : array|\PDOStatement|string|\think\Model

查找单条记录 不存在则返回空模型

Parameters

array|string|\think\db\Query|\Closure $data

Throws

\think\exception\DbException
\think\db\exception\ModelNotFoundException
\think\db\exception\DataNotFoundException

Returns

array|\PDOStatement|string|\think\Model —

chunk()

chunk(int  $count, callable  $callback, string|array  $column = null, string  $order = 'asc') : bool

分批数据返回处理

Parameters

int $count

每次处理的数据数量

callable $callback

处理回调方法

string|array $column

分批处理的字段名

string $order

字段排序

Throws

\think\exception\DbException

Returns

bool —

getBind()

getBind(bool  $clear = true) : array

获取绑定的参数 并清空

Parameters

bool $clear

Returns

array —

buildSql()

buildSql(bool  $sub = true) : string

创建子查询SQL

Parameters

bool $sub

Throws

\think\exception\DbException

Returns

string —

parsePkWhere()

parsePkWhere(array|string  $data) : void

把主键值转换为查询条件 支持复合主键

Parameters

array|string $data

主键数据

Throws

\think\Exception

event()

event(string  $event, callable  $callback) : void

注册回调方法

Parameters

string $event

事件名

callable $callback

回调方法

trigger()

trigger(string  $event) : bool

触发事件

Parameters

string $event

事件名

Returns

bool —

lazyWrite()

lazyWrite(string  $type, string  $guid, int  $step, int  $lazyTime) : false|int

延时更新检查 返回false表示需要延时 否则返回实际写入的数值

Parameters

string $type

自增或者自减

string $guid

写入标识

int $step

写入步进值

int $lazyTime

延时时间(s)

Returns

false|int —

getJoinTable()

getJoinTable(array|string  $join, string  $alias = null) : string

获取Join表名及别名 支持 ['prefix_table或者子查询'=>'alias'] 'table alias'

Parameters

array|string $join
string $alias

Returns

string —

bindParams()

bindParams(string  $sql, array  $bind = []) : void

参数绑定

Parameters

string $sql

绑定的sql表达式

array $bind

参数绑定

parseWhereExp()

parseWhereExp(string  $logic, mixed  $field, mixed  $op, mixed  $condition, array  $param = [], bool  $strict = false) : $this

分析查询表达式

Parameters

string $logic

查询逻辑 and or xor

mixed $field

查询字段

mixed $op

查询表达式

mixed $condition

查询条件

array $param

查询参数

bool $strict

严格模式

Returns

$this —

parseWhereItem()

parseWhereItem(string  $logic, mixed  $field, mixed  $op, mixed  $condition, array  $param = []) : mixed

分析查询表达式

Parameters

string $logic

查询逻辑 and or xor

mixed $field

查询字段

mixed $op

查询表达式

mixed $condition

查询条件

array $param

查询参数

Returns

mixed —

parseArrayWhereItems()

parseArrayWhereItems(array  $field, string  $logic) : $this

数组批量查询

Parameters

array $field

批量查询

string $logic

查询逻辑 and or xor

Returns

$this —

options()

options(array  $options) : $this

查询参数赋值

Parameters

array $options

表达式参数

Returns

$this —

withAggregate()

withAggregate(string|array  $relation, string  $aggregate = 'count', string  $field = '*', bool  $subQuery = true) : $this

关联统计

Parameters

string|array $relation

关联方法名

string $aggregate

聚合查询方法

string $field

字段

bool $subQuery

是否使用子查询

Returns

$this —

resultSetToModelCollection()

resultSetToModelCollection(array  $resultSet) : \think\model\Collection

查询数据转换为模型数据集对象

Parameters

array $resultSet

数据集

Returns

\think\model\Collection —

resultSet()

resultSet(array  $resultSet) : void

处理数据集

Parameters

array $resultSet

resultToEmpty()

resultToEmpty() : array|\think\Model|null

处理空数据

Throws

\think\exception\DbException
\think\db\exception\ModelNotFoundException
\think\db\exception\DataNotFoundException

Returns

array|\think\Model|null —

parseQuery()

parseQuery(mixed  $data, string  $with, bool  $cache) : \think\db\Query

分析查询表达式

Parameters

mixed $data

主键列表或者查询条件(闭包)

string $with

关联预查询

bool $cache

是否缓存

Returns

\think\db\Query —

result()

result(array  $result) : void

处理数据

Parameters

array $result

查询数据

getResultAttr()

getResultAttr(array  $result, array  $withAttr = []) : void

使用获取器处理数据

Parameters

array $result

查询数据

array $withAttr

字段获取器

jsonResult()

jsonResult(array  $result, array  $json = [], bool  $assoc = false, array  $withRelationAttr = []) : void

JSON字段数据转换

Parameters

array $result

查询数据

array $json

JSON字段

bool $assoc

是否转换为数组

array $withRelationAttr

关联获取器

resultToModel()

resultToModel(array  $result, array  $options = [], bool  $resultSet = false, array  $withRelationAttr = []) : void

查询数据转换为模型对象

Parameters

array $result

查询数据

array $options

查询参数

bool $resultSet

是否为数据集查询

array $withRelationAttr

关联字段获取器

getModelUpdateCondition()

getModelUpdateCondition(array  $options) : mixed

获取模型的更新条件

Parameters

array $options

查询参数

Returns

mixed —

throwNotFound()

throwNotFound(array  $options = []) : mixed

查询失败 抛出异常

Parameters

array $options

查询参数

Throws

\think\db\exception\ModelNotFoundException
\think\db\exception\DataNotFoundException

Returns

mixed —

parseView()

parseView(array  $options) : void

视图查询处理

Parameters

array $options

查询参数

parseOptions()

parseOptions() : array

分析表达式(可用于查询或者写入操作)

Returns

array —