PHP Class think\Model

Afficher le fichier Open project: top-think/framework Class Usage Examples

Protected Properties

Свойство 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 显示属性

Méthodes publiques

Méthode 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 = [] ) 设置需要输出的属性

Méthodes protégées

Méthode 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 méthode

public __call ( $method, $args )

__callStatic() public static méthode

public static __callStatic ( $method, $params )

__construct() public méthode

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

__get() public méthode

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

__isset() public méthode

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

__set() public méthode

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

__toString() public méthode

public __toString ( )

__unset() public méthode

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

__wakeup() public méthode

解序列化后处理
public __wakeup ( )

all() public static méthode

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

allowField() public méthode

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

append() public méthode

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

auto() public méthode

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

autoCompleteData() protected méthode

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

autoWriteTimestamp() protected méthode

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

belongsTo() public méthode

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类型
Résultat think\model\Relation

belongsToMany() public méthode

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 别名定义
Résultat think\model\Relation

create() public static méthode

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

data() public méthode

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

db() public méthode

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

delete() public méthode

删除当前的记录
public delete ( ) : integer
Résultat integer

destroy() public static méthode

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

eagerlyResult() public méthode

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

eagerlyResultSet() public méthode

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

event() public static méthode

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

get() public static méthode

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

getAttr() public méthode

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

getData() public méthode

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

getDb() protected static méthode

protected static getDb ( )

getError() public méthode

返回模型的错误信息
public getError ( ) : string
Résultat string

getPk() public méthode

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

has() public static méthode

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

hasMany() public méthode

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

hasManyThrough() public méthode

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 别名定义
Résultat think\model\Relation

hasOne() public méthode

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类型
Résultat think\model\Relation

hasWhere() public static méthode

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

hidden() public méthode

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

init() protected static méthode

初始化处理
protected static init ( ) : void
Résultat void

initialize() protected méthode

初始化模型
protected initialize ( ) : void
Résultat void

isPk() protected méthode

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

isUpdate() public méthode

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

jsonSerialize() public méthode

JsonSerializable
public jsonSerialize ( )

offsetExists() public méthode

public offsetExists ( $name )

offsetGet() public méthode

public offsetGet ( $name )

offsetSet() public méthode

ArrayAccess
public offsetSet ( $name, $value )

offsetUnset() public méthode

public offsetUnset ( $name )

parseModel() protected méthode

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

parseQuery() protected static méthode

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

readTransform() protected méthode

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

relation() protected méthode

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

relationQuery() public méthode

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

save() public méthode

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

saveAll() public méthode

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

scope() public static méthode

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

setAttr() public méthode

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

toArray() public méthode

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

toJson() public méthode

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

trigger() protected méthode

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

update() public static méthode

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

useGlobalScope() public static méthode

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

validate() public méthode

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

validateData() protected méthode

自动验证数据
protected validateData ( array $data ) : boolean
$data array 验证数据
Résultat boolean

validateFailException() public méthode

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

visible() public méthode

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

writeTransform() protected méthode

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

Property Details

$append protected_oe property

追加属性
protected $append

$auto protected_oe property

保存自动完成列表
protected $auto

$autoWriteTimestamp protected_oe property

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

$change protected_oe property

记录改变字段
protected $change

$class protected_oe property

当前类名称
protected $class

$connection protected_oe property

数据库配置
protected $connection

$createTime protected_oe property

创建时间字段
protected $createTime

$data protected_oe property

数据信息
protected $data

$dateFormat protected_oe property

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

$error protected_oe property

错误信息
protected $error

$failException protected_oe property

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

$field protected_oe property

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

$hidden protected_oe property

隐藏属性
protected $hidden

$initialized protected_oe static_oe property

初始化过的模型.
protected static array $initialized
Résultat array

$insert protected_oe property

新增自动完成列表
protected $insert

$isUpdate protected_oe property

是否为更新数据
protected $isUpdate

$name protected_oe property

当前模型名称
protected $name

$pk protected_oe property

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

$query protected_oe property

数据库查询对象
protected $query

$readonly protected_oe property

只读字段
protected $readonly

$relation protected_oe property

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

$table protected_oe property

数据表名称
protected $table

$type protected_oe property

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

$update protected_oe property

更新自动完成列表
protected $update

$updateTime protected_oe property

更新时间字段
protected $updateTime

$updateWhere protected_oe property

更新条件
protected $updateWhere

$useGlobalScope protected_oe property

全局查询范围
protected $useGlobalScope

$validate protected_oe property

字段验证规则
protected $validate

$visible protected_oe property

显示属性
protected $visible