企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] 模型的方法由 >[info] >* vendor\topthink\think-orm\src\Model.php abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonable JsonSerializable.jsonSerialize()json_encode(model对象)时触发 ArrayAccess:数组方式操作model对象 Arrayable:toArray()方法 Jsonable:toJson()方法 >Model的 trait类: >* vendor\topthink\think-orm\src\model\concern\Attribute.php >vendor\topthink\think-orm\src\model\concern\RelationShip.php >* vendor\topthink\think-orm\src\model\concern\ModelEvent.php >* vendor\topthink\think-orm\src\model\concern\TimeStamp.php >* vendor\topthink\think-orm\src\model\concern\Conversion.php 以及Db(facade隐射到了DbManager)、BaseQuery类构成 BaseQuery引入: use concern\TimeFieldQuery; use concern\AggregateQuery; use concern\ModelRelationQuery; use concern\ResultOperation; use concern\Transaction; use concern\WhereQuery; ## **model属性** 属性除了注明的其他都为protected | 属性名 | 默认值 | 描述 | 类型 | --- | --- | --- | ---| | | <h2 style="color:blue">Model</h2> | | | | $suffix | none| 数据表后缀 | string| | $connection| none| 数据库配置 | string| | $name| none| 模型名称(不加前缀) | string| | $key| none| 主键值 | string| | $table| none| 数据表名称(需要加前缀) | string| | $initialized| []| 初始化过的模型(静态属性) | array| | $queryInstance| none| 查询对象实例 | Query| | $defaultSoftDelete| none| 软删除字段默认值 | mixed| | $globalScope| []| 全局查询范围 | array| | $db| none| Db对象 | DbManager(Db)| | $invoker | none| 容器对象的依赖注入方法 | callable| | $maker| none| 服务注入(静态属性 ) | Closure[](Closure:闭包函数)| | $macro| [] | 方法注入 | Closure[][]| | | <h2 style="color:blue">Attribute </h2> | | | | 属性 | 默认值 | 描述 | 类型 | | $pk | id | 数据表主键 复合主键使用数组定义 | string或array | | $schema | [] | 数据表字段信息 留空则自动获取 | array | | $field | [] | 当前允许写入的字段 | array | | $type| [] | 字段自动类型转换 | array | | $disuse| [] | 数据表废弃字段 | array | | $readonly| [] | 数据表废弃字段 | array | | $json| [] |JSON数据表字段| array | | $jsonType| [] |JSON数据表字段类型| array | | $jsonAssoc| false |JSON数据取出是否需要转换为数组| bool | | $strict| true|是否严格字段大小写| bool | | | <h2 style="color:blue">RelationShip</h2> | | | | 属性 | 默认值 | 描述 | 类型 | | relationWrite | [] | 关联自动写入信息| array| | | <h2 style="color:blue">ModelEvent</h2> | | | | 属性 | 默认值 | 描述 | 类型 | | $event | none | Event对象(静态属性) | object | | $withEvent | true | 是否需要事件响应 | bool | | | <h2 style="color:blue">TimeStamp</h2> | | | | 属性 | 默认值 | 描述 | 类型 | | $autoWriteTimestamp | none |是否需要自动写入时间,值为布尔true/false则自动识别,如果设置为字符串 则表示时间字段的类型;支持的字段类型包括int/timestamp/datetime/date |bool或string | | $createTime | create_time | 创建时间字段 false表示关闭 | false或string | | $updateTime | update_time | 更新时间字段 false表示关闭 | false或|string | | $dateFormat | none | 取出数据时间字段显示格式,此项可在database.php数据库配置文件设置默认:"Y-m-d H:i:s" | string | | | <h2 style="color:blue">Conversion</h2> | | | | 属性 | 默认值 | 描述 | 类型 | | $visible | [] | 数据输出显示的属性 | array | | $hidden| [] | 数据输出隐藏的属性 | array | | $append| [] | 数据输出需要追加的属性 | array | | $resultSetType | none | 数据集对象名 | string | ## **model方法** | 方法名(public) | 描述 | 参数 | 返回值| | --- | --- | --- | ---| | | <h2 style="color:blue">Model</h2> | | | | maker | 设置服务注入(静态) | (Closure $maker) |void| | macro | 设置方法注入(静态) | (string $method, Closure $closure) |void| | setDb | 设置Db对象 | (DbManager $db) |void| | setInvoker | 设置容器对象及依赖注入方法 | (callable $callable) | void | | invoke | 调用反射执行模型方法 支持参数绑定 | (string $method, array $vars = []) | mixed | | getName | 获取当前模型名称 。先找name属性,初始化时name没有设置则赋值为当前运行模块的类名如(User) | void | string | | newInstance | 创建新的模型实例 | (array $data = [], $where = null) | Model | | setUpdateWhere | 设置模型的更新条件(protected) | ($where) | void | | setConnection | 设置当前模型的数据库连接 |(string $connection) | $this | | getConnection| 获取当前模型的数据库连接标识 |(void ) | string| | connect| 切换数据库连接进行查询(静态方法) |(string $connection) | Model| | ~~setQuery~~(<span style="color:red">X</span>) |设置当前模型的数据库查询对象| (Query $query, bool $clear = true) | $this | | ~~getQuery~~(<span style="color:red">X</span>) | 获取当前模型的数据库查询对象 | void | Query或者null | | setSuffix | 设置当前模型数据表的后缀 | (string $suffix) | $this | | getSuffix | 获取当前模型的数据表后缀 | void | string | | suffix | 切换表后缀进行查询(静态方法) | (string $suffix) | Model | | db | 获取当前模型的数据库查询对象 | ($scope = []) | Query | | force | 更新是否强制写入数据 而不做比较(亦可用于软删除的强制删除) | (bool $force = true) | $this | | isForce | 判断force | void | bool | | replace | 新增数据是否使用Replace | (bool $replace = true) | $this | | refresh | 刷新模型数据 | (bool $relation = false) | $this | | exists | 设置数据是否存在 | (bool $exists = true) | $this | | isExists | 判断数据是否存在数据库 | void | bool | |isEmpty | 判断模型是否为空 | void | bool | | lazySave | 延迟保存当前数据对象 | ($data = []) | void | | getWhere | 获取当前的更新条件 | void | mixed | | checkAllowFields(protected)| 检查数据是否允许写入 | (void) | array| | updateData| 保存写入数据(protected) | (void) | bool | | insertData| 新增写入数据(protected) | (string $sequence = null) | bool | | save | 保存当前数据对象(入库操作) | (array $data = [], string $sequence = null) | bool | | saveAll | 保存多个数据到当前数据对象(入库操作) | (iterable $dataSet, bool $replace = true) | Collection| | delete | 删除当前的记录 | void |bool | | create | 写入数据(静态方法) | (array $data, array $allowField = [], bool $replace = false) | Model | | update | 更新数据(静态方法) | (array $data, $where = [], array $allowField = []) | static(注意区分self) | | destroy | 删除记录(静态方法) | ($data, bool $force = false) | bool | |__wakeup | 解序列化后处理 | void | | |__isset| 检测数据对象的值 | (string $name) | bool | |__unset| 销毁数据对象的值 | (string $name) | void | |~~__debugInfo~~(<span style="color:red">X</span>) | | | | | __set | 修改器原型 设置数据对象的值 | (string $name, $value) | void | | __get | 获取器原型 获取数据对象的值 | (string $name) | mixed | | __call | 调用不存在的方法时触发(先macro方法,再获取器方法,再Query方法) |($method, $args) | mixed | | __callStatic | 调用不存在的静态方法时触发(先macro方法,再Query方法) |($method, $args) | mixed | | withoutGlobalScope | 设置不使用的全局查询范围(静态方法) | (array $scope = null) | Query | | 可覆盖的扩展方法名(protected)| 描述 | 参数 | 返回值| | init | 自定义的初始化处理 | void | 自定义| | checkData | | | 自定义| | checkResult | | ($result) | 自定义| | | <h2 style="color:blue">Attribute</h2> | | | | getKey | 获取模型对象的主键值 | void | mixed | | allowField | 设置允许写入的字段 | (array $field) | $this | | readOnly | 设置只读字段 | (array $field) | $this | | data | 设置数据对象值 | (array $data, bool $set = false, array $allow = []) | $this | | appendData | 批量追加数据对象值 | (array $data, bool $set = false) | $this | | getOrigin | 获取对象原始数据 如果不存在指定字段返回null | (string $name = null) | mixed | | getData | 获取对象原始数据 如果不存在指定字段返回false | (string $name = null) | mixed | | getChangedData | 获取变化的数据 并排除只读数据 | void | array | | set | 直接设置数据对象值 | (string $name, $value) | void | | setAttrs | 通过修改器 批量设置数据对象值 | (array $data) | void | | setAttr | 通过修改器 设置数据对象值 | (string $name, $value, array $data = []) | void | | getAttr | 获取器 获取数据对象的值 | (string $name) | mixed | | withAttribute | 设置数据字段获取器 | ($name, callable $callback = null) | $this | | isPk |判断一个字段名是否为主键字段(protected)|(string $key)|bool| | getRealFieldName(protected) | 获取实际的字段名 | (string $name) | string | | writeTransform(protected) | 数据写入 类型转换(protected) | ($value, $type) | mixed | | getValue | 获取经过获取器处理后的数据对象的值(protected) | (string $name, $value, $relation = false) | mixed | | getJsonValue | 获取JSON字段属性值(protected) | ($name, $value) | mixed | | getRelationValue | 获取关联属性值(protected) |(string $relation) | mixed | | readTransform | 数据读取 类型转换(protected) |($value, $type) | mixed | | | <h2 style="color:blue">RelationShip</h2> | | | | 方法名 | 描述 | 参数 | 返回值 | | setParent | 设置父关联对象 | (Model $model) | $this | | getParent | 获取父关联对象 | void | Model | | getRelation | 获取当前模型的关联模型数据 | (string $name = null, bool $auto = false) | mixed | | setRelation | 设置关联数据对象值 | (string $name, $value, array $data = []) | $this | | relationQuery | 查询当前模型的关联数据 | (array $relations, array $withRelationAttr = []) | void | | together | 关联数据写入 | (array $relation) | $this | | has(静态) | 根据关联条件查询当前模型 | (string $relation, string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '') | Query | | hasWhere(静态) | 根据关联条件查询当前模型 | (string $relation, $where = [], string $fields = '*', string $joinType = '') | Query | | eagerly | 预载入关联查询 JOIN方式 | (Query $query, string $relation, $field, string $joinType = '', Closure $closure = null, bool $first = false) | bool | | eagerlyResultSet | 预载入关联查询 返回数据集 | (array &$resultSet, array $relations, array $withRelationAttr = [], bool $join = false, $cache = false) | void | | eagerlyResult | 预载入关联查询 返回模型对象 | (Model $result, array $relations, array $withRelationAttr = [], bool $join = false, $cache = false) | void | | bindAttr | 绑定(一对一)关联属性到当前模型 | (string $relation, array $attrs = []) | $this | | relationCount | 关联统计 | (Model $result, array $relations, string $aggregate = 'sum', string $field = '*') | void | | hasOne | HAS ONE 关联定义 | hasOne(string $model, string $foreignKey = '', string $localKey = '') | HasOne | | belongsTo | BELONGS TO 关联定义 | (string $model, string $foreignKey = '', string $localKey = '') | BelongsTo | | hasMany | HAS MANY 关联定义 | (string $model, string $foreignKey = '', string $localKey = '') | HasMany | | hasManyThrough | HAS MANY 远程关联定义 | (string $model, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = '') | HasManyThrough | | hasOneThrough | HAS ONE 远程关联定义 | hasOneThrough(string $model, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = '') | HasOneThrough | | belongsToMany | BELONGS TO MANY 关联定义 | (string $model, string $middle = '', string $foreignKey = '', string $localKey = '') | BelongsToMany | | morphOne | MORPH One 关联定义 | (string $model, $morph = null, string $type = '') | MorphOne | | morphMany | MORPH MANY 关联定义 | (string $model, $morph = null, string $type = '') | MorphMany | | morphTo | MORPH TO 关联定义 | ($morph = null, array $alias = []) | MorphTo | | morphByMany | MORPH BY MANY关联定义 | (string $model, string $middle, $morph = null, string $foreignKey = null) | MorphToMany | | removeRelation | 移除当前模型的关联属性 | void | $this | | parseModel | 解析模型的完整命名空间(protected) | (string $model) | string | | getForeignKey | 获取模型的默认外键名(protected) | (string $name) | string | | isRelationAttr |检查属性是否为关联属性 如果是则返回关联方法名(protected) | (string $attr) | string|false | | getRelationData |智能获取关联模型数据(protected) | (Relation $modelRelation) | mixed | | checkAutoRelationWrite | 关联数据自动写入检查(protected) | (void) | void | | autoRelationUpdate | 自动关联数据更新(针对一对一关联 protected) | (void) | void | | autoRelationInsert | 自动关联数据写入(针对一对一关联 protected) | (void) | void | | autoRelationDelete | 自动关联数据删除(针对一对一关联 protected) | (void) | void | | | <h2 style="color:blue">ModelEvent</h2> | | | | setEvent | 设置Event对象(静态方法) | (object $event) | void | | withEvent | 当前操作的事件响应 | (bool $is_event_response) | $this | | trigger | 触发事件(protected) |(string $event) | bool | | | <h2 style="color:blue">TimeStamp</h2> | | | | isAutoWriteTimestamp | 是否需要自动写入时间字段(调用checkTimeFieldType方法) | ($auto="bool or string") | $this | | getAutoWriteTimestamp | 获取自动写入时间字段 | void | bool 或 string | | setDateFormat | 设置时间字段格式化 | ($format='string or false') | $this | | getDateFormat | 获取自动写入时间字段 | void | string 或 false | | checkTimeFieldType | 检测时间字段的实际类型(protected) | ($type) | mixed | | autoWriteTimestamp | 自动写入时间戳(protected) | (void) | mixed | | getTimeTypeValue | 获取指定类型的时间字段值(protected) | (string $type) | mixed | | formatDateTime | 时间日期字段格式化处理(protected) | ($format, $time = 'now', bool $timestamp = false) | mixed | | getTimestampValue | 获取时间字段值(protected) |($value) | mixed | | | <h2 style="color:blue">Conversion</h2> | | | | append | 设置需要附加的输出属性 | (array $append = []) | $this | | appendRelationAttr | 设置附加关联对象的属性 | (string $attr, array $append) | $this | | hidden | 设置需要隐藏的输出属性 | (array $hidden = []) |$this | | visible | 设置需要输出的属性 | (array $visible = []) | $this | | toArray | 转换当前模型对象为数组 | void | array | | toJson| 转换当前模型对象为JSON字符串 | (int $options = JSON_UNESCAPED_UNICODE) | string| | __toString| model类对象被当做字符串输出(echo、print输出时转换当前模型对象为JSON字符串) | void | string | | jsonSerialize | json格式的当前模型对象转数组格式 | void | array | | toCollection| 转换数据集为数据集对象 | (iterable $collection = [], string $resultSetType = null) | Collection | | appendAttrToArray| | | | | getBindAttr| | | | # **特殊** ## **SoftDelete软删除trait类** 要使用软删除功能,需要引入`SoftDelete`trait,例如`User`模型按照下面的定义就可以使用软删除功能: ~~~ namespace app\model; use think\Model; use think\model\concern\SoftDelete; class User extends Model { use SoftDelete; protected $deleteTime = 'delete_time'; } ~~~ | SoftDelete属性名 | 默认值 | 描述 | 类型 | --- | --- | --- | ---| | | <h2 style="color:blue">Model</h2> | | | | $withTrashed | false| 是否包含软删除数据 | bool| | SoftDelete方法名(public) | 描述 | 参数 | 返回值| | --- | --- | --- | ---| | | <h2 style="color:blue">Model</h2> | | | | trashed | 判断当前实例是否被软删除 | (void) |bool| | withTrashed | 查询软删除数据(静态) | (void) |Query| | onlyTrashed | 只查询软删除数据(静态) | (void) |Query| | destroy | 删除记录(静态) | ($data, bool $force = false) |bool| | delete | 删除当前的记录 | (void) |bool| | restore | 恢复被软删除的记录 | ($where = []) |bool| | withTrashedData | 是否包含软删除数据(protected) |(bool $withTrashed) |this| | getWithTrashedExp | 获取软删除数据的查询条件(protected) |(void) |array| | getDeleteTimeField | 获取软删除字段(protected) |(bool $read = false) |string\|false| | withNoTrashed| 查询的时候默认排除软删除数据(protected) |(Query $query) |void| ## **OptimLock乐观锁trait类** 这个使用前也要引用??? | OptimLock方法名(public) | 描述 | 参数 | 返回值| | --- | --- | --- | ---| | | <h2 style="color:blue">Model</h2> | | | | getWhere | | (void) |string?| | getOptimLockField | (protected) | (void) |string?| | checkData | 数据检查(protected) | (void) |void| | recordLockVersion | 记录乐观锁(protected) | (void) |void| | updateLockVersion | 更新乐观锁(protected) | (void) |void| | checkResult | (protected) | ($result) |void| ## -------------------------------------------------------------------------------------------------- * @method void onAfterRead(Model $model) static after_read事件定义 * @method mixed onBeforeInsert(Model $model) static before_insert事件定义 * @method void onAfterInsert(Model $model) static after_insert事件定义 * @method mixed onBeforeUpdate(Model $model) static before_update事件定义 * @method void onAfterUpdate(Model $model) static after_update事件定义 * @method mixed onBeforeWrite(Model $model) static before_write事件定义 * @method void onAfterWrite(Model $model) static after_write事件定义 * @method mixed onBeforeDelete(Model $model) static before_write事件定义 * @method void onAfterDelete(Model $model) static after_delete事件定义 * @method void onBeforeRestore(Model $model) static before_restore事件定义 * @method void onAfterRestore(Model $model) static after_restore事件定义 >[danger]**hasOne**与**BelongsTo**继承至OneToOne **HasOneThrough**继承HasManyThrough **MorphToMany**继承BelongsToMany **OneToOne**、**HasMany**、**HasManyThrough**、**BelongsToMany**、**MorphMany**、**MorphOne**、**MorphTo**继承至**Relation** >[danger]以下关联类只收集public ## **Relation(基类)** 模型关联基础类 | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | ## 方法 | | | | getParent | 获取关联的所属模型 | void | Model | | getModel | 获取当前的关联模型对象实例 | void | Model | | getQuery | 获取关联的查询对象 | void | Query | | ~~selfRelation~~ | 设置当前关联为自关联 | ($self = true) | this | | isSelfRelation | 当前关联是否为自关联 | void | bool | | update | 更新数据 | (array $data = []) | int | | delete | 删除记录 |($data = null)| int | | withLimit | 限制关联数据的数量 | (int $limit) |this | | withField| 限制关联数据的字段 |(array $field) | this | | __call | 获取Query中的方法 | ($method, $args) | mixed | ## **OneToOne** | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | ## 方法| | | | joinType | 设置join类型 | ($type) | this | | eagerly | 预载入关联查询(JOIN方式) | (Query $query, string $relation, $field = true, string $joinType = '', Closure $closure = null, bool $first = false) | void | | eagerlyResultSet | 预载入关联查询(数据集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = [], bool $join = false) | void | | eagerlyResult | 预载入关联查询(数据) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = [], bool $join = false) | void | | save | 保存(新增)当前关联数据对象 | ($data, bool $replace = true) | Model \| false | | ~~setEagerlyType~~ | 设置预载入方式 | ($type) | this | | ~~getEagerlyType~~ | 获取预载入方式 | void | integer | | bind | 绑定关联表的属性到父模型属性 | ($attr) | this | | getBindAttr | 获取绑定属性 | void | array | | ~~relationCount~~| 关联统计 | ($result, $closure) | integer | | | **抽象方法(protected)** | | | | eagerlySet | || | | eagerlyOne | || | ## **HasOne** | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | **方法** | | | | getRelation | 延迟获取关联数据 | ($subRelation = '', $closure = null) |Model | | getRelationCountQuery | 创建关联统计子查询 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string | | relationCount | 关联统计 | (Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer | | has | 根据关联条件查询当前模型 | (string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '', Query $query = null) | Query | | hasWhere | 根据关联条件查询当前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query | ## **BelongsTo** | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | ## 方法| | | | getRelation | 延迟获取关联数据 | (array $subRelation = [], Closure $closure = null) | Model | | getRelationCountQuery | 创建关联统计子查询 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string | | relationCount | 关联统计 | (Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer | | has | 根据关联条件查询当前模型 | (string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '', Query $query = null) | Query | | hasWhere | 根据关联条件查询当前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query | | associate | 添加关联数据 | (Model $model) | Model | | dissociate | 注销关联数据 | void | Model | ## **HasMany** | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | **方法**| | | | getRelation | 延迟获取关联数据 | (array $subRelation = [], Closure $closure = null) | Collection | | eagerlyResultSet | 预载入关联查询(数据集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | | eagerlyResult | 预载入关联查询(数据) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | | has | 根据关联条件查询当前模型 | has(string $operator = '>=', int $count = 1, string $id = '*', string $joinType = 'INNER', Query $query = null) | Query | | hasWhere | 根据关联条件查询当前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query | | relationCount | 关联统计 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer | | getRelationCountQuery | 创建关联统计子查询 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string | | save | 保存(新增)当前关联数据对象 | ($data, bool $replace = true) | Model \| false | | make | 创建关联对象实例 | ($data = \[\]) | Model | | saveAll | 批量保存当前关联数据对象 | (iterable $dataSet, bool $replace = true) | integer | ## **HasManyThrough** | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | **方法**| | | | getRelation | 延迟获取关联数据 | (array $subRelation = [], Closure $closure = null) | Collection | | eagerlyResultSet | 预载入关联查询(数据集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | | eagerlyResult | 预载入关联查询(数据) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | | has | 根据关联条件查询当前模型 | (string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '', Query $query = null) | Query | | hasWhere | 根据关联条件查询当前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query | | relationCount | 关联统计 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer | | getRelationCountQuery | 创建关联统计子查询 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string | ## **HasManyThrough** HasOneThrough的基类 | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | **方法**| | | | getRelation | 延迟获取关联数据 | (array $subRelation = [], Closure $closure = null) | Collection | | eagerlyResultSet | 预载入关联查询(数据集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | | eagerlyResult | 预载入关联查询(数据) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | ## **HasOneThrough** | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | **方法**| | | | getRelation | 延迟获取关联数据 | (array $subRelation = [], Closure $closure = null) | Model | | eagerlyResultSet | 预载入关联查询(数据集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | | eagerlyResult | 预载入关联查询(数据) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | ## **MorphMany** 多态一对多关联 | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | **方法**| | | | getRelation | 延迟获取关联数据 | (array $subRelation = [], Closure $closure = null) | Collection | | eagerlyResultSet | 预载入关联查询(数据集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | | eagerlyResult | 预载入关联查询(数据) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | | has | 根据关联条件查询当前模型 | has(string $operator = '>=', int $count = 1, string $id = '*', string $joinType = 'INNER', Query $query = null) | Query | | hasWhere | 根据关联条件查询当前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query | | relationCount | 关联统计 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer | | getRelationCountQuery | 创建关联统计子查询 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string | | save | 保存(新增)当前关联数据对象 | ($data, bool $replace = true) | Model \| false | | make | 创建关联对象实例 | ($data = \[\]) | Model | | saveAll | 批量保存当前关联数据对象 | (iterable $dataSet, bool $replace = true) | integer | ## **MorphOne** 多态一对一关联类 | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | **方法**| | | | getRelation | 延迟获取关联数据 | (array $subRelation = [], Closure $closure = null) | Collection | | eagerlyResultSet | 预载入关联查询(数据集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | | eagerlyResult | 预载入关联查询(数据) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | | has | 根据关联条件查询当前模型 | has(string $operator = '>=', int $count = 1, string $id = '*', string $joinType = 'INNER', Query $query = null) | Query | | hasWhere | 根据关联条件查询当前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query | | relationCount | 关联统计 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer | | getRelationCountQuery | 创建关联统计子查询 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string | | save | 保存(新增)当前关联数据对象 | ($data, bool $replace = true) | Model \| false | | make | 创建关联对象实例 | ($data = \[\]) | Model | ## **MorphTo** 多态关联类 | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | **方法**| | | | getModel | 获取当前的关联模型类的实例 | void | Model | | getRelation | 延迟获取关联数据 | (array $subRelation = [], Closure $closure = null) | Model | | has | 根据关联条件查询当前模型 | (string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '', Query $query = null) | Query | | hasWhere | 这里不支持抛出错误 | | | | setAlias | 设置多态别名 |(array $alias) | this | | removeOption | 移除关联查询参数 | void | this | | eagerlyResultSet | 预载入关联查询(数据集) | (array &$resultSet, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | | eagerlyResult | 预载入关联查询(数据) | (Model $result, string $relation, array $subRelation = [], Closure $closure = null, array $cache = []) | void | | relationCount | 关联统计 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer | | associate | 添加关联数据 | (Model $model, string $type = '') | Model | | dissociate | 注销关联数据 | void | Model | ## **BelongsToMany** | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | **方法**| | | | pivot | 设置中间表模型 | (string $pivot) | this | | name | 设置中间表数据名称 | (string $name) | this | | select | 重载select方法 | ($data = null) | Collection | | paginate | 重载paginate方法 | ($listRows = null, $simple = false) | Paginator | | find | 重载find方法 | ($data = null) | Model | | selectOrFail |查找多条记录 如果不存在则抛出异常 | ($data = null) | Collection | | findOrFail | 查找单条记录 如果不存在则抛出异常 | ($data = null) | Model | | getRelation | 延迟获取关联数据 | (array $subRelation = [], Closure $closure = null) | Collection | | has | 根据关联条件查询当前模型 | (string $operator = '>=', $count = 1, $id = '*', string $joinType = 'INNER', Query $query = null) | Query | | hasWhere | 根据关联条件查询当前模型 | ($where = [], $fields = null, string $joinType = '', Query $query = null) | Query | | wherePivot | 设置中间表的查询条件 | ($field, $op = null, $condition = null) | this | | eagerlyResultSet | 预载入关联查询(数据集) | (array &$resultSet, string $relation, array $subRelation, Closure $closure = null, array $cache = []) | void | | eagerlyResult | 预载入关联查询(数据) | (Model $result, string $relation, array $subRelation, Closure $closure = null, array $cache = []) | void | | relationCount | 关联统计 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer | | getRelationCountQuery | 创建关联统计子查询 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string | | save | 保存(新增)当前关联数据对象 | ($data, array $pivot = []) | array \| Pivot | | saveAll| 批量保存当前关联数据对象 | (iterable $dataSet, array $pivot = [], bool $samePivot = false) | array \| false| | attach| 附加关联的一个中间表数据 | ($data, array $pivot = []) | array \| Pivot | | attached| 判断是否存在关联数据 | ($data) | array \| false | | detach| 解除关联的一个中间表数据 | ($data = null, bool $relationDel = false) | integer | | sync| 数据同步 | (array $ids, bool $detaching = true) | array | ## **MorphToMany** | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | **方法**| | | | eagerlyResultSet | 预载入关联查询(数据集) | (array &$resultSet, string $relation, array $subRelation, Closure $closure = null, array $cache = []) | void | | eagerlyResult | 预载入关联查询(数据) | (Model $result, string $relation, array $subRelation, Closure $closure = null, array $cache = []) | void | | relationCount | 关联统计 |(Model $result, Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | integer | | getRelationCountQuery | 创建关联统计子查询 | (Closure $closure = null, string $aggregate = 'count', string $field = '*', string &$name = null) | string | | attach| 附加关联的一个中间表数据 | ($data, array $pivot = []) | array \| Pivot | | attached| 判断是否存在关联数据 | ($data) | array \| false | | detach| 解除关联的一个中间表数据 | ($data = null, bool $relationDel = false) | integer | | sync| 数据同步 | (array $ids, bool $detaching = true) | array | ## **ModelRelationQuery** | 方法名(public) | 描述 | 参数 | 返回值 | | --- | --- | --- | --- | | | **方法**| | | | model | 指定模型对象实例 | (Model $model) | $this | | getModel | 获取当前的模型对象 | void | Model|null | | hidden | 设置需要隐藏的输出属性 | (array $hidden) | this | | visible | 设置需要输出的属性 | (array $visible) | (array $hidden) | | append | 设置需要追加输出的属性 | (array $append) | this | | scope | 添加查询范围 | ($scope, ...$args) | this | | relation | 设置关联查询 | (array $relation) | this | | withSearch | 使用搜索器条件搜索字段 | ($fields, $data = [], string $prefix = '') | this | | withAttr | 设置数据字段获取器 | ($name, callable $callback = null) | this | | with | 关联预载入 In方式,参数是关联方法名,参数可以是数组或者字符串 | ($with) | this | | withJoin | 关联预载入 JOIN方式 | ($with, string $joinType = '') | this | | withCache | 关联缓存 | withCache($relation = true, $key = true, $expire = null, string $tag = null) | this | | withCount | 关联统计 | ($relation, bool $subQuery = true) | this | | withSum | 关联统计Sum | ($relation, string $field, bool $subQuery = true) | this | | withMax | 关联统计Max | ($relation, string $field, bool $subQuery = true) | this | | withMin | 关联统计Min | ($relation, string $field, bool $subQuery = true) | this | | withAvg | 关联统计Avg | ($relation, string $field, bool $subQuery = true) | this | | has | 根据关联条件查询当前模型 | (string $relation, string $operator = '>=', int $count = 1, string $id = '*', string $joinType = '') | this | | hasWhere | 根据关联条件查询当前模型 | (string $relation, $where = [], string $fields = '*', string $joinType = '') | this | >$subQuery参数 是否使用子查询 $relation 关联方法名 ``` //protected static $macro = []; public static function macro(string $method, Closure $closure) { if (!isset(static::$macro[static::class])) { static::$macro[static::class] = []; } static::$macro[static::class][$method] = $closure; } public function __call($method, $args) { if (isset(static::$macro[static::class][$method])) { return call_user_func_array(static::$macro[static::class][$method]->bindTo($this, static::class), $args); } if ('withattr' == strtolower($method)) { return call_user_func_array([$this, 'withAttribute'], $args); } return call_user_func_array([$this->db(), $method], $args); } public static function __callStatic($method, $args) { if (isset(static::$macro[static::class][$method])) { return call_user_func_array(static::$macro[static::class][$method]->bindTo(null, static::class), $args); } $model = new static(); return call_user_func_array([$model->db(), $method], $args); } ```