\app\api\modelAdmin

Admin 模型 Class Admin

Summary

Methods
Properties
Constants
__construct()
readMaster()
newInstance()
getQuery()
db()
setParent()
getParent()
data()
getData()
isAutoWriteTimestamp()
force()
setAttr()
getRelation()
setRelation()
getAttr()
append()
appendRelationAttr()
hidden()
visible()
toArray()
toJson()
removeRelation()
toCollection()
together()
getPk()
replace()
save()
getChangedData()
setInc()
setDec()
saveAll()
allowField()
except()
readonly()
isUpdate()
delete()
auto()
validate()
validateFailException()
getError()
event()
create()
update()
get()
all()
destroy()
useGlobalScope()
has()
hasWhere()
relationQuery()
eagerlyResultSet()
eagerlyResult()
relationCount()
hasOne()
belongsTo()
hasMany()
hasManyThrough()
belongsToMany()
morphMany()
morphOne()
morphTo()
__call()
__callStatic()
__set()
__get()
__isset()
__unset()
__toString()
jsonSerialize()
offsetSet()
offsetExists()
offsetUnset()
offsetGet()
__wakeup()
group()
No public properties found
No constants found
buildQuery()
initialize()
init()
autoWriteTimestamp()
formatDateTime()
writeTransform()
getRelationData()
readTransform()
parseAttr()
subToArray()
isPk()
checkAllowField()
autoRelationUpdate()
getWhere()
autoCompleteData()
validateData()
trigger()
parseQuery()
parseModel()
getForeignKey()
beforeInsert()
afterInsert()
beforeUpdate()
afterUpdate()
beforeWrite()
afterWrite()
beforeDelete()
afterDelete()
$links
$connection
$parent
$query
$name
$table
$class
$error
$validate
$pk
$field
$except
$disuse
$readonly
$visible
$hidden
$append
$data
$origin
$relation
$auto
$insert
$update
$autoWriteTimestamp
$createTime
$updateTime
$dateFormat
$type
$isUpdate
$replace
$force
$updateWhere
$failException
$useGlobalScope
$batchValidate
$resultSetType
$relationWrite
$initialized
$readMaster
N/A
No private methods found
$event
N/A

Properties

$links

$links

$connection

$connection

$parent

$parent

$query

$query

$name

$name

$table

$table

$class

$class

$error

$error

$validate

$validate

$pk

$pk

$field

$field

$except

$except

$disuse

$disuse

$readonly

$readonly

$visible

$visible

$hidden

$hidden

$append

$append

$data

$data

$origin

$origin

$relation

$relation

$auto

$auto

$insert

$insert

$update

$update

$autoWriteTimestamp

$autoWriteTimestamp : bool

开启自动写入时间戳

Type

bool

$createTime

$createTime

$updateTime

$updateTime

$dateFormat

$dateFormat : string

Type

string — 时间输出格式

$type

$type : string[]

Type

string[] — 数据写入时类型转换

$isUpdate

$isUpdate

$replace

$replace

$force

$force

$updateWhere

$updateWhere

$failException

$failException

$useGlobalScope

$useGlobalScope

$batchValidate

$batchValidate

$resultSetType

$resultSetType

$relationWrite

$relationWrite

$initialized

$initialized : array

初始化过的模型.

Type

array

$readMaster

$readMaster : array

是否从主库读取(主从分布式有效)

Type

array

$event

$event

Methods

__construct()

__construct(array|object  $data = []) : mixed

构造方法

Parameters

array|object $data

数据

Returns

mixed —

readMaster()

readMaster(bool  $all = false) : $this

是否从主库读取数据(主从分布有效)

Parameters

bool $all

是否所有模型生效

Returns

$this —

newInstance()

newInstance(array|object  $data = [], bool  $isUpdate = false, mixed  $where = null) : \think\Model

创建新的模型实例

Parameters

array|object $data

数据

bool $isUpdate

是否为更新

mixed $where

更新条件

Returns

\think\Model —

getQuery()

getQuery(bool  $buildNewQuery = false) : \think\db\Query

获取当前模型的查询对象

Parameters

bool $buildNewQuery

创建新的查询对象

Returns

\think\db\Query —

db()

db(bool  $useBaseQuery = true, bool  $buildNewQuery = true) : \think\db\Query

获取当前模型的数据库查询对象

Parameters

bool $useBaseQuery

是否调用全局查询范围

bool $buildNewQuery

创建新的查询对象

Returns

\think\db\Query —

setParent()

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

设置父关联对象

Parameters

\think\Model $model

模型对象

Returns

$this —

getParent()

getParent() : \think\Model

获取父关联对象

Returns

\think\Model —

data()

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

设置数据对象值

Parameters

mixed $data

数据或者属性名

mixed $value

Returns

$this —

getData()

getData(string  $name = null) : mixed

获取对象原始数据 如果不存在指定字段返回false

Parameters

string $name

字段名 留空获取全部

Throws

\InvalidArgumentException

Returns

mixed —

isAutoWriteTimestamp()

isAutoWriteTimestamp(bool  $auto) : $this

是否需要自动写入时间字段

Parameters

bool $auto

Returns

$this —

force()

force(bool  $force = true) : $this

更新是否强制写入数据 而不做比较

Parameters

bool $force

Returns

$this —

setAttr()

setAttr(string  $name, mixed  $value, array  $data = []) : $this

修改器 设置数据对象值

Parameters

string $name

属性名

mixed $value

属性值

array $data

数据

Returns

$this —

getRelation()

getRelation(string  $name = null) : mixed

获取当前模型的关联模型数据

Parameters

string $name

关联方法名

Returns

mixed —

setRelation()

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

设置关联数据对象值

Parameters

string $name

属性名

mixed $value

属性值

Returns

$this —

getAttr()

getAttr(string  $name) : mixed

获取器 获取数据对象的值

Parameters

string $name

名称

Throws

\InvalidArgumentException

Returns

mixed —

append()

append(array  $append = [], bool  $override = false) : $this

设置需要追加的输出属性

Parameters

array $append

属性列表

bool $override

是否覆盖

Returns

$this —

appendRelationAttr()

appendRelationAttr(string  $relation, string|array  $append) : $this

设置附加关联对象的属性

Parameters

string $relation

关联方法

string|array $append

追加属性名

Throws

\think\Exception

Returns

$this —

hidden()

hidden(array  $hidden = [], bool  $override = false) : $this

设置需要隐藏的输出属性

Parameters

array $hidden

属性列表

bool $override

是否覆盖

Returns

$this —

visible()

visible(array  $visible = [], bool  $override = false) : $this

设置需要输出的属性

Parameters

array $visible
bool $override

是否覆盖

Returns

$this —

toArray()

toArray() : array

转换当前模型对象为数组

Returns

array —

toJson()

toJson(int  $options = JSON_UNESCAPED_UNICODE) : string

转换当前模型对象为JSON字符串

Parameters

int $options

json参数

Returns

string —

removeRelation()

removeRelation() : $this

移除当前模型的关联属性

Returns

$this —

toCollection()

toCollection(array|\think\Collection  $collection) : \think\Collection

转换当前模型数据集为数据集对象

Parameters

array|\think\Collection $collection

数据集

Returns

\think\Collection —

together()

together(mixed  $relation) : $this

关联数据一起更新

Parameters

mixed $relation

关联

Returns

$this —

getPk()

getPk(string  $name = '') : mixed

获取模型对象的主键

Parameters

string $name

模型名

Returns

mixed —

replace()

replace(bool  $replace = true) : $this

新增数据是否使用Replace

Parameters

bool $replace

Returns

$this —

save()

save(array  $data = [], array  $where = [], string  $sequence = null) : int|false

保存当前数据对象

Parameters

array $data

数据

array $where

更新条件

string $sequence

自增序列名

Returns

int|false —

getChangedData()

getChangedData() : array

获取变化的数据 并排除只读数据

Returns

array —

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 —

saveAll()

saveAll(array  $dataSet, bool  $replace = true) : array|false

保存多个数据到当前数据对象

Parameters

array $dataSet

数据

bool $replace

是否自动识别更新和写入

Throws

\Exception

Returns

array|false —

allowField()

allowField(string|array  $field) : $this

设置允许写入的字段

Parameters

string|array $field

允许写入的字段 如果为true只允许写入数据表字段

Returns

$this —

except()

except(string|array  $field) : $this

设置排除写入的字段

Parameters

string|array $field

排除允许写入的字段

Returns

$this —

readonly()

readonly(mixed  $field) : $this

设置只读字段

Parameters

mixed $field

只读字段

Returns

$this —

isUpdate()

isUpdate(bool  $update = true, mixed  $where = null) : $this

是否为更新数据

Parameters

bool $update
mixed $where

Returns

$this —

delete()

delete() : int

删除当前的记录

Returns

int —

auto()

auto(array  $fields) : $this

设置自动完成的字段( 规则通过修改器定义)

Parameters

array $fields

需要自动完成的字段

Returns

$this —

validate()

validate(array|string|bool  $rule = true, array  $msg = [], bool  $batch = false) : $this

设置字段验证

Parameters

array|string|bool $rule

验证规则 true表示自动读取验证器类

array $msg

提示信息

bool $batch

批量验证

Returns

$this —

validateFailException()

validateFailException(bool  $fail = true) : $this

设置验证失败后是否抛出异常

Parameters

bool $fail

是否抛出异常

Returns

$this —

getError()

getError() : string|array

返回模型的错误信息

Returns

string|array —

event()

event(string  $event, callable  $callback, bool  $override = false) : void

注册回调方法

Parameters

string $event

事件名

callable $callback

回调方法

bool $override

是否覆盖

create()

create(array  $data = [], array|true  $field = null) : $this

写入数据

Parameters

array $data

数据数组

array|true $field

允许字段

Returns

$this —

update()

update(array  $data = [], array  $where = [], array|true  $field = null) : $this

更新数据

Parameters

array $data

数据数组

array $where

更新条件

array|true $field

允许字段

Returns

$this —

get()

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

查找单条记录

Parameters

mixed $data

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

array|string $with

关联预查询

bool $cache

是否缓存

Throws

\think\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\exception\DbException

Returns

static[]|false —

destroy()

destroy(mixed  $data) : int

删除记录

Parameters

mixed $data

主键列表 支持闭包查询条件

Returns

int —

成功删除的记录数

useGlobalScope()

useGlobalScope(bool  $use) : \think\db\Query

设置是否使用全局查询范围

Parameters

bool $use

是否启用全局查询范围

Returns

\think\db\Query —

has()

has(string  $relation, mixed  $operator = '>=', int  $count = 1, string  $id = '*') : \think\model\Relation|\think\db\Query

根据关联条件查询当前模型

Parameters

string $relation

关联方法名

mixed $operator

比较操作符

int $count

个数

string $id

关联表的统计字段

Returns

\think\model\Relation|\think\db\Query —

hasWhere()

hasWhere(string  $relation, mixed  $where = [], mixed  $fields = null) : \think\model\Relation|\think\db\Query

根据关联条件查询当前模型

Parameters

string $relation

关联方法名

mixed $where

查询条件(数组或者闭包)

mixed $fields

字段

Returns

\think\model\Relation|\think\db\Query —

relationQuery()

relationQuery(string|array  $relations) : $this

查询当前模型的关联数据

Parameters

string|array $relations

关联名

Returns

$this —

eagerlyResultSet()

eagerlyResultSet(array  $resultSet, string  $relation) : array

预载入关联查询 返回数据集

Parameters

array $resultSet

数据集

string $relation

关联名

Returns

array —

eagerlyResult()

eagerlyResult(\think\Model  $result, string  $relation) : \think\Model

预载入关联查询 返回模型对象

Parameters

\think\Model $result

数据对象

string $relation

关联名

Returns

\think\Model —

relationCount()

relationCount(\think\Model  $result, string|array  $relation) : void

关联统计

Parameters

\think\Model $result

数据对象

string|array $relation

关联名

hasOne()

hasOne(string  $model, string  $foreignKey = '', string  $localKey = '', array  $alias = [], string  $joinType = 'INNER') : \think\model\relation\HasOne

HAS ONE 关联定义

Parameters

string $model

模型名

string $foreignKey

关联外键

string $localKey

当前模型主键

array $alias

别名定义(已经废弃)

string $joinType

JOIN类型

Returns

\think\model\relation\HasOne —

belongsTo()

belongsTo(string  $model, string  $foreignKey = '', string  $localKey = '', array  $alias = [], string  $joinType = 'INNER') : \think\model\relation\BelongsTo

BELONGS TO 关联定义

Parameters

string $model

模型名

string $foreignKey

关联外键

string $localKey

关联主键

array $alias

别名定义(已经废弃)

string $joinType

JOIN类型

Returns

\think\model\relation\BelongsTo —

hasMany()

hasMany(string  $model, string  $foreignKey = '', string  $localKey = '') : \think\model\relation\HasMany

HAS MANY 关联定义

Parameters

string $model

模型名

string $foreignKey

关联外键

string $localKey

当前模型主键

Returns

\think\model\relation\HasMany —

hasManyThrough()

hasManyThrough(string  $model, string  $through, string  $foreignKey = '', string  $throughKey = '', string  $localKey = '') : \think\model\relation\HasManyThrough

HAS MANY 远程关联定义

Parameters

string $model

模型名

string $through

中间模型名

string $foreignKey

关联外键

string $throughKey

关联外键

string $localKey

当前模型主键

Returns

\think\model\relation\HasManyThrough —

belongsToMany()

belongsToMany(string  $model, string  $table = '', string  $foreignKey = '', string  $localKey = '') : \think\model\relation\BelongsToMany

BELONGS TO MANY 关联定义

Parameters

string $model

模型名

string $table

中间表名

string $foreignKey

关联外键

string $localKey

当前模型关联键

Returns

\think\model\relation\BelongsToMany —

morphMany()

morphMany(string  $model, string|array  $morph = null, string  $type = '') : \think\model\relation\MorphMany

MORPH MANY 关联定义

Parameters

string $model

模型名

string|array $morph

多态字段信息

string $type

多态类型

Returns

\think\model\relation\MorphMany —

morphOne()

morphOne(string  $model, string|array  $morph = null, string  $type = '') : \think\model\relation\MorphOne

MORPH One 关联定义

Parameters

string $model

模型名

string|array $morph

多态字段信息

string $type

多态类型

Returns

\think\model\relation\MorphOne —

morphTo()

morphTo(string|array  $morph = null, array  $alias = []) : \think\model\relation\MorphTo

MORPH TO 关联定义

Parameters

string|array $morph

多态字段信息

array $alias

多态别名定义

Returns

\think\model\relation\MorphTo —

__call()

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

Parameters

mixed $method
mixed $args

Returns

mixed —

__callStatic()

__callStatic(mixed  $method, mixed  $args) : mixed

Parameters

mixed $method
mixed $args

Returns

mixed —

__set()

__set(string  $name, mixed  $value) : void

修改器 设置数据对象的值

Parameters

string $name

名称

mixed $value

__get()

__get(string  $name) : mixed

获取器 获取数据对象的值

Parameters

string $name

名称

Returns

mixed —

__isset()

__isset(string  $name) : bool

检测数据对象的值

Parameters

string $name

名称

Returns

bool —

__unset()

__unset(string  $name) : void

销毁数据对象的值

Parameters

string $name

名称

__toString()

__toString() : mixed

Returns

mixed —

jsonSerialize()

jsonSerialize() : mixed

Returns

mixed —

offsetSet()

offsetSet(mixed  $name, mixed  $value) : mixed

Parameters

mixed $name
mixed $value

Returns

mixed —

offsetExists()

offsetExists(mixed  $name) : mixed

Parameters

mixed $name

Returns

mixed —

offsetUnset()

offsetUnset(mixed  $name) : mixed

Parameters

mixed $name

Returns

mixed —

offsetGet()

offsetGet(mixed  $name) : mixed

Parameters

mixed $name

Returns

mixed —

__wakeup()

__wakeup() : mixed

解序列化后处理

Returns

mixed —

group()

group() : \app\api\model\Admin|array|false|string|\think\model\relation\BelongsTo

Admin关联AdminRole

Returns

\app\api\model\Admin|array|false|string|\think\model\relation\BelongsTo —

buildQuery()

buildQuery() : \think\db\Query

创建模型的查询对象

Returns

\think\db\Query —

initialize()

initialize() : void

初始化模型

init()

init() : void

初始化处理

autoWriteTimestamp()

autoWriteTimestamp(string  $name) : mixed

自动写入时间戳

Parameters

string $name

时间戳字段

Returns

mixed —

formatDateTime()

formatDateTime(mixed  $time, mixed  $format, bool  $timestamp = false) : mixed

时间日期字段格式化处理

Parameters

mixed $time

时间日期表达式

mixed $format

日期格式

bool $timestamp

是否进行时间戳转换

Returns

mixed —

writeTransform()

writeTransform(mixed  $value, string|array  $type) : mixed

数据写入 类型转换

Parameters

mixed $value

string|array $type

要转换的类型

Returns

mixed —

getRelationData()

getRelationData(\think\model\Relation  $modelRelation) : mixed

获取关联模型数据

Parameters

\think\model\Relation $modelRelation

模型关联对象

Throws

\BadMethodCallException

Returns

mixed —

readTransform()

readTransform(mixed  $value, string|array  $type) : mixed

数据读取 类型转换

Parameters

mixed $value

string|array $type

要转换的类型

Returns

mixed —

parseAttr()

parseAttr(array  $attrs, array  $result, bool  $visible = true) : array

解析隐藏及显示属性

Parameters

array $attrs

属性

array $result

结果集

bool $visible

Returns

array —

subToArray()

subToArray(\think\Model|\think\model\Collection  $model,  $visible,  $hidden,  $key) : array

转换子模型对象

Parameters

\think\Model|\think\model\Collection $model
$visible
$hidden
$key

Returns

array —

isPk()

isPk(string  $key) : bool

判断一个字段名是否为主键字段

Parameters

string $key

名称

Returns

bool —

checkAllowField()

checkAllowField(mixed  $auto = []) : mixed

Parameters

mixed $auto

Returns

mixed —

autoRelationUpdate()

autoRelationUpdate(mixed  $relation) : mixed

Parameters

mixed $relation

Returns

mixed —

getWhere()

getWhere() : mixed

获取更新条件

Returns

mixed —

autoCompleteData()

autoCompleteData(array  $auto = []) : void

数据自动完成

Parameters

array $auto

要自动更新的字段列表

validateData()

validateData(array  $data, mixed  $rule = null, bool  $batch = null) : bool

自动验证数据

Parameters

array $data

验证数据

mixed $rule

验证规则

bool $batch

批量验证

Returns

bool —

trigger()

trigger(string  $event, mixed  $params) : bool

触发事件

Parameters

string $event

事件名

mixed $params

传入参数(引用)

Returns

bool —

parseQuery()

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

分析查询表达式

Parameters

mixed $data

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

string $with

关联预查询

bool $cache

是否缓存

Returns

\think\db\Query —

parseModel()

parseModel(string  $model) : string

解析模型的完整命名空间

Parameters

string $model

模型名(或者完整类名)

Returns

string —

getForeignKey()

getForeignKey(string  $name) : string

获取模型的默认外键名

Parameters

string $name

模型名

Returns

string —

beforeInsert()

beforeInsert( $callback, bool  $override = false) : mixed

模型事件快捷方法

Parameters

$callback
bool $override

Returns

mixed —

afterInsert()

afterInsert(mixed  $callback, mixed  $override = false) : mixed

Parameters

mixed $callback
mixed $override

Returns

mixed —

beforeUpdate()

beforeUpdate(mixed  $callback, mixed  $override = false) : mixed

Parameters

mixed $callback
mixed $override

Returns

mixed —

afterUpdate()

afterUpdate(mixed  $callback, mixed  $override = false) : mixed

Parameters

mixed $callback
mixed $override

Returns

mixed —

beforeWrite()

beforeWrite(mixed  $callback, mixed  $override = false) : mixed

Parameters

mixed $callback
mixed $override

Returns

mixed —

afterWrite()

afterWrite(mixed  $callback, mixed  $override = false) : mixed

Parameters

mixed $callback
mixed $override

Returns

mixed —

beforeDelete()

beforeDelete(mixed  $callback, mixed  $override = false) : mixed

Parameters

mixed $callback
mixed $override

Returns

mixed —

afterDelete()

afterDelete(mixed  $callback, mixed  $override = false) : mixed

Parameters

mixed $callback
mixed $override

Returns

mixed —