PHP Class think\Model

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

Protected Properties

Property Type Description
$append 追加属性
$auto 保存自动完成列表
$autoWriteTimestamp 是否需要自动写入时间戳 如果设置为字符串 则表示时间字段的类型
$change 记录改变字段
$class 当前类名称
$connection 数据库配置
$createTime 创建时间字段
$data 数据信息
$dateFormat 时间字段取出后的默认时间格式
$error 错误信息
$failException 验证失败是否抛出异常
$field 数据表字段信息 留空则自动获取
$hidden 隐藏属性
$initialized array 初始化过的模型.
$insert 新增自动完成列表
$isUpdate 是否为更新数据
$links 数据库对象池
$name 当前模型名称
$pk 数据表主键 复合主键使用数组定义 不设置则自动获取
$query 数据库查询对象
$readonly 只读字段
$relation 当前执行的关联对象
$table 数据表名称
$type 字段类型或者格式转换
$update 更新自动完成列表
$updateTime 更新时间字段
$updateWhere 更新条件
$useGlobalScope 全局查询范围
$validate 字段验证规则
$visible 显示属性

Public Methods

Method Description
__call ( $method, $args )
__callStatic ( $method, $params )
__construct ( array | object $data = [] ) 架构函数
__get ( string $name ) : mixed 获取器 获取数据对象的值
__isset ( string $name ) : boolean 检测数据对象的值
__set ( string $name, mixed $value ) : void 修改器 设置数据对象的值
__toString ( )
__unset ( string $name ) : void 销毁数据对象的值
__wakeup ( ) 解序列化后处理
all ( mixed $data = null, array | string $with = [], boolean $cache = false ) : static[] | false 查找所有记录
allowField ( boolean | array $field ) 设置允许写入的字段
append ( array $append = [] ) 设置需要追加的输出属性
auto ( array $fields ) 设置自动完成的字段( 规则通过修改器定义)
belongsTo ( string $model, string $foreignKey = '', string $otherKey = '', array $alias = [], string $joinType = 'INNER' ) : Relation BELONGS TO 关联定义
belongsToMany ( string $model, string $table = '', string $foreignKey = '', string $localKey = '', array $alias = [] ) : Relation BELONGS TO MANY 关联定义
create ( array $data = [] ) 写入数据
data ( mixed $data, mixed $value = null ) 设置数据对象值
db ( boolean $baseQuery = true ) : Query 获取当前模型的数据库查询对象
delete ( ) : integer 删除当前的记录
destroy ( mixed $data ) : integer 删除记录
eagerlyResult ( Model $result, string $relation ) : Model 预载入关联查询 返回模型对象
eagerlyResultSet ( array $resultSet, string $relation ) : array 预载入关联查询 返回数据集
event ( string $event, callable $callback, boolean $override = false ) : void 注册回调方法
get ( mixed $data = null, array | string $with = [], boolean $cache = false ) : static 查找单条记录
getAttr ( string $name ) : mixed 获取器 获取数据对象的值
getData ( string $name = null ) : mixed 获取对象原始数据 如果不存在指定字段返回false
getError ( ) : string 返回模型的错误信息
getPk ( string $name = '' ) : mixed 获取模型对象的主键
has ( string $relation, string $operator = '>=', integer $count = 1, string $id = '*' ) : Model 根据关联条件查询当前模型
hasMany ( string $model, string $foreignKey = '', string $localKey = '', array $alias = [] ) : Relation HAS MANY 关联定义
hasManyThrough ( string $model, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', array $alias = [] ) : Relation HAS MANY 远程关联定义
hasOne ( string $model, string $foreignKey = '', string $localKey = '', array $alias = [], string $joinType = 'INNER' ) : Relation HAS ONE 关联定义
hasWhere ( string $relation, mixed $where = [] ) : Model 根据关联条件查询当前模型
hidden ( array $hidden = [] ) 设置需要隐藏的输出属性
isUpdate ( boolean $update = true, mixed $where = null ) 是否为更新数据
jsonSerialize ( ) JsonSerializable
offsetExists ( $name )
offsetGet ( $name )
offsetSet ( $name, $value ) ArrayAccess
offsetUnset ( $name )
relationQuery ( string | array $relations ) 查询当前模型的关联数据
save ( array $data = [], array $where = [], string $sequence = null ) : integer | false 保存当前数据对象
saveAll ( array $dataSet, boolean $replace = true ) : array | false 保存多个数据到当前数据对象
scope ( string | array | Closure $name ) : Model 命名范围
setAttr ( string $name, mixed $value, array $data = [] ) 修改器 设置数据对象值
toArray ( ) : array 转换当前模型对象为数组
toJson ( integer $options = JSON_UNESCAPED_UNICODE ) : string 转换当前模型对象为JSON字符串
update ( array $data = [], array $where = [] ) 更新数据
useGlobalScope ( boolean $use ) : Model 设置是否使用全局查询范围
validate ( array | string | boolean $rule = true, array $msg = [] ) 设置字段验证
validateFailException ( boolean $fail = true ) 设置验证失败后是否抛出异常
visible ( array $visible = [] ) 设置需要输出的属性

Protected Methods

Method Description
autoCompleteData ( array $auto = [] ) : void 数据自动完成
autoWriteTimestamp ( string $name ) : mixed 自动写入时间戳
getDb ( )
init ( ) : void 初始化处理
initialize ( ) : void 初始化模型
isPk ( string $key ) : boolean 判断一个字段名是否为主键字段
parseModel ( string $model ) : string 解析模型的完整命名空间
parseQuery ( mixed &$data, string $with, boolean $cache ) : Query 分析查询表达式
readTransform ( mixed $value, string | array $type ) : mixed 数据读取 类型转换
relation ( string | array $relation = null ) : Relation | Query 获取关联模型实例
trigger ( string $event, mixed &$params ) : boolean 触发事件
validateData ( array $data ) : boolean 自动验证数据
writeTransform ( mixed $value, string | array $type ) : mixed 数据写入 类型转换

Method Details

__call() public method

public __call ( $method, $args )

__callStatic() public static method

public static __callStatic ( $method, $params )

__construct() public method

架构函数
public __construct ( array | object $data = [] )
$data array | object 数据

__get() public method

获取器 获取数据对象的值
public __get ( string $name ) : mixed
$name string 名称
return mixed

__isset() public method

检测数据对象的值
public __isset ( string $name ) : boolean
$name string 名称
return boolean

__set() public method

修改器 设置数据对象的值
public __set ( string $name, mixed $value ) : void
$name string 名称
$value mixed
return void

__toString() public method

public __toString ( )

__unset() public method

销毁数据对象的值
public __unset ( string $name ) : void
$name string 名称
return void

__wakeup() public method

解序列化后处理
public __wakeup ( )

all() public static method

查找所有记录
public static all ( mixed $data = null, array | string $with = [], boolean $cache = false ) : static[] | false
$data mixed 主键列表或者查询条件(闭包)
$with array | string 关联预查询
$cache boolean 是否缓存
return static[] | false

allowField() public method

设置允许写入的字段
public allowField ( boolean | array $field )
$field boolean | array 允许写入的字段 如果为true只允许写入数据表字段

append() public method

设置需要追加的输出属性
public append ( array $append = [] )
$append array 属性列表

auto() public method

设置自动完成的字段( 规则通过修改器定义)
public auto ( array $fields )
$fields array 需要自动完成的字段

autoCompleteData() protected method

数据自动完成
protected autoCompleteData ( array $auto = [] ) : void
$auto array 要自动更新的字段列表
return void

autoWriteTimestamp() protected method

自动写入时间戳
protected autoWriteTimestamp ( string $name ) : mixed
$name string 时间戳字段
return mixed

belongsTo() public method

BELONGS TO 关联定义
public belongsTo ( string $model, string $foreignKey = '', string $otherKey = '', array $alias = [], string $joinType = 'INNER' ) : Relation
$model string 模型名
$foreignKey string 关联外键
$otherKey string 关联主键
$alias array 别名定义
$joinType string JOIN类型
return think\model\Relation

belongsToMany() public method

BELONGS TO MANY 关联定义
public belongsToMany ( string $model, string $table = '', string $foreignKey = '', string $localKey = '', array $alias = [] ) : Relation
$model string 模型名
$table string 中间表名
$foreignKey string 关联外键
$localKey string 当前模型关联键
$alias array 别名定义
return think\model\Relation

create() public static method

写入数据
public static create ( array $data = [] )
$data array 数据数组

data() public method

设置数据对象值
public data ( mixed $data, mixed $value = null )
$data mixed 数据或者属性名
$value mixed

db() public method

获取当前模型的数据库查询对象
public db ( boolean $baseQuery = true ) : Query
$baseQuery boolean 是否调用全局查询范围
return Think\Db\Query

delete() public method

删除当前的记录
public delete ( ) : integer
return integer

destroy() public static method

删除记录
public static destroy ( mixed $data ) : integer
$data mixed 主键列表 支持闭包查询条件
return integer 成功删除的记录数

eagerlyResult() public method

预载入关联查询 返回模型对象
public eagerlyResult ( Model $result, string $relation ) : Model
$result Model 数据对象
$relation string 关联名
return Model

eagerlyResultSet() public method

预载入关联查询 返回数据集
public eagerlyResultSet ( array $resultSet, string $relation ) : array
$resultSet array 数据集
$relation string 关联名
return array

event() public static method

注册回调方法
public static event ( string $event, callable $callback, boolean $override = false ) : void
$event string 事件名
$callback callable 回调方法
$override boolean 是否覆盖
return void

get() public static method

查找单条记录
public static get ( mixed $data = null, array | string $with = [], boolean $cache = false ) : static
$data mixed 主键值或者查询条件(闭包)
$with array | string 关联预查询
$cache boolean 是否缓存
return static

getAttr() public method

获取器 获取数据对象的值
public getAttr ( string $name ) : mixed
$name string 名称
return mixed

getData() public method

获取对象原始数据 如果不存在指定字段返回false
public getData ( string $name = null ) : mixed
$name string 字段名 留空获取全部
return mixed

getDb() protected static method

protected static getDb ( )

getError() public method

返回模型的错误信息
public getError ( ) : string
return string

getPk() public method

获取模型对象的主键
public getPk ( string $name = '' ) : mixed
$name string 模型名
return mixed

has() public static method

根据关联条件查询当前模型
public static has ( string $relation, string $operator = '>=', integer $count = 1, string $id = '*' ) : Model
$relation string 关联方法名
$operator string 比较操作符
$count integer 个数
$id string 关联表的统计字段
return Model

hasMany() public method

HAS MANY 关联定义
public hasMany ( string $model, string $foreignKey = '', string $localKey = '', array $alias = [] ) : Relation
$model string 模型名
$foreignKey string 关联外键
$localKey string 关联主键
$alias array 别名定义
return think\model\Relation

hasManyThrough() public method

HAS MANY 远程关联定义
public hasManyThrough ( string $model, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', array $alias = [] ) : Relation
$model string 模型名
$through string 中间模型名
$foreignKey string 关联外键
$throughKey string 关联外键
$localKey string 关联主键
$alias array 别名定义
return think\model\Relation

hasOne() public method

HAS ONE 关联定义
public hasOne ( string $model, string $foreignKey = '', string $localKey = '', array $alias = [], string $joinType = 'INNER' ) : Relation
$model string 模型名
$foreignKey string 关联外键
$localKey string 关联主键
$alias array 别名定义
$joinType string JOIN类型
return think\model\Relation

hasWhere() public static method

根据关联条件查询当前模型
public static hasWhere ( string $relation, mixed $where = [] ) : Model
$relation string 关联方法名
$where mixed 查询条件(数组或者闭包)
return Model

hidden() public method

设置需要隐藏的输出属性
public hidden ( array $hidden = [] )
$hidden array 属性列表

init() protected static method

初始化处理
protected static init ( ) : void
return void

initialize() protected method

初始化模型
protected initialize ( ) : void
return void

isPk() protected method

判断一个字段名是否为主键字段
protected isPk ( string $key ) : boolean
$key string 名称
return boolean

isUpdate() public method

是否为更新数据
public isUpdate ( boolean $update = true, mixed $where = null )
$update boolean
$where mixed

jsonSerialize() public method

JsonSerializable
public jsonSerialize ( )

offsetExists() public method

public offsetExists ( $name )

offsetGet() public method

public offsetGet ( $name )

offsetSet() public method

ArrayAccess
public offsetSet ( $name, $value )

offsetUnset() public method

public offsetUnset ( $name )

parseModel() protected method

解析模型的完整命名空间
protected parseModel ( string $model ) : string
$model string 模型名(或者完整类名)
return string

parseQuery() protected static method

分析查询表达式
protected static parseQuery ( mixed &$data, string $with, boolean $cache ) : Query
$data mixed 主键列表或者查询条件(闭包)
$with string 关联预查询
$cache boolean 是否缓存
return Think\Db\Query

readTransform() protected method

数据读取 类型转换
protected readTransform ( mixed $value, string | array $type ) : mixed
$value mixed
$type string | array 要转换的类型
return mixed

relation() protected method

获取关联模型实例
protected relation ( string | array $relation = null ) : Relation | Query
$relation string | array 关联查询
return think\model\Relation | Think\Db\Query

relationQuery() public method

查询当前模型的关联数据
public relationQuery ( string | array $relations )
$relations string | array 关联名

save() public method

保存当前数据对象
public save ( array $data = [], array $where = [], string $sequence = null ) : integer | false
$data array 数据
$where array 更新条件
$sequence string 自增序列名
return integer | false

saveAll() public method

保存多个数据到当前数据对象
public saveAll ( array $dataSet, boolean $replace = true ) : array | false
$dataSet array 数据
$replace boolean 是否自动识别更新和写入
return array | false

scope() public static method

命名范围
public static scope ( string | array | Closure $name ) : Model
$name string | array | Closure 命名范围名称 逗号分隔
return Model

setAttr() public method

修改器 设置数据对象值
public setAttr ( string $name, mixed $value, array $data = [] )
$name string 属性名
$value mixed 属性值
$data array 数据

toArray() public method

转换当前模型对象为数组
public toArray ( ) : array
return array

toJson() public method

转换当前模型对象为JSON字符串
public toJson ( integer $options = JSON_UNESCAPED_UNICODE ) : string
$options integer json参数
return string

trigger() protected method

触发事件
protected trigger ( string $event, mixed &$params ) : boolean
$event string 事件名
$params mixed 传入参数(引用)
return boolean

update() public static method

更新数据
public static update ( array $data = [], array $where = [] )
$data array 数据数组
$where array 更新条件

useGlobalScope() public static method

设置是否使用全局查询范围
public static useGlobalScope ( boolean $use ) : Model
$use boolean 是否启用全局查询范围
return Model

validate() public method

设置字段验证
public validate ( array | string | boolean $rule = true, array $msg = [] )
$rule array | string | boolean 验证规则 true表示自动读取验证器类
$msg array 提示信息

validateData() protected method

自动验证数据
protected validateData ( array $data ) : boolean
$data array 验证数据
return boolean

validateFailException() public method

设置验证失败后是否抛出异常
public validateFailException ( boolean $fail = true )
$fail boolean 是否抛出异常

visible() public method

设置需要输出的属性
public visible ( array $visible = [] )
$visible array

writeTransform() protected method

数据写入 类型转换
protected writeTransform ( mixed $value, string | array $type ) : mixed
$value mixed
$type string | array 要转换的类型
return mixed

Property Details

$append protected property

追加属性
protected $append

$auto protected property

保存自动完成列表
protected $auto

$autoWriteTimestamp protected property

是否需要自动写入时间戳 如果设置为字符串 则表示时间字段的类型
protected $autoWriteTimestamp

$change protected property

记录改变字段
protected $change

$class protected property

当前类名称
protected $class

$connection protected property

数据库配置
protected $connection

$createTime protected property

创建时间字段
protected $createTime

$data protected property

数据信息
protected $data

$dateFormat protected property

时间字段取出后的默认时间格式
protected $dateFormat

$error protected property

错误信息
protected $error

$failException protected property

验证失败是否抛出异常
protected $failException

$field protected property

数据表字段信息 留空则自动获取
protected $field

$hidden protected property

隐藏属性
protected $hidden

$initialized protected static property

初始化过的模型.
protected static array $initialized
return array

$insert protected property

新增自动完成列表
protected $insert

$isUpdate protected property

是否为更新数据
protected $isUpdate

$name protected property

当前模型名称
protected $name

$pk protected property

数据表主键 复合主键使用数组定义 不设置则自动获取
protected $pk

$query protected property

数据库查询对象
protected $query

$readonly protected property

只读字段
protected $readonly

$relation protected property

当前执行的关联对象
protected $relation

$table protected property

数据表名称
protected $table

$type protected property

字段类型或者格式转换
protected $type

$update protected property

更新自动完成列表
protected $update

$updateTime protected property

更新时间字段
protected $updateTime

$updateWhere protected property

更新条件
protected $updateWhere

$useGlobalScope protected property

全局查询范围
protected $useGlobalScope

$validate protected property

字段验证规则
protected $validate

$visible protected property

显示属性
protected $visible