Properties

$joinType

$joinType : string

JOIN类型

Type

string

$bindAttr

$bindAttr : array

绑定的关联属性

Type

array

$relation

$relation : string

关联名

Type

string

$parent

$parent : \think\Model

父模型对象

Type

Model

$model

$model : string

当前关联的模型类名

Type

string

$query

$query : \think\db\BaseQuery

关联模型查询对象

Type

BaseQuery

$foreignKey

$foreignKey : string

关联表外键

Type

string

$localKey

$localKey : string

关联表主键

Type

string

$baseQuery

$baseQuery : bool

是否执行关联基础查询

Type

bool

$selfRelation

$selfRelation : bool

是否为自关联

Type

bool

$withLimit

$withLimit : int

关联数据数量限制

Type

int

$withField

$withField : array

关联数据字段限制

Type

array

Methods

joinType()

joinType(string  $type) : $this

设置join类型

Parameters

string $type

JOIN类型

Returns

$this —

eagerly()

eagerly(\think\db\BaseQuery  $query, string  $relation, mixed  $field = true, string  $joinType = '', \Closure  $closure = null, bool  $first = false) : void

预载入关联查询(JOIN方式)

Parameters

\think\db\BaseQuery $query

查询对象

string $relation

关联名

mixed $field

关联字段

string $joinType

JOIN方式

\Closure $closure

闭包条件

bool $first

eagerlyResultSet()

eagerlyResultSet(array  $resultSet, string  $relation, array  $subRelation = [], \Closure  $closure = null, array  $cache = [], bool  $join = false) : void

预载入关联查询(数据集)

Parameters

array $resultSet

数据集

string $relation

当前关联名

array $subRelation

子关联名

\Closure $closure

闭包

array $cache

关联缓存

bool $join

是否为JOIN方式

eagerlyResult()

eagerlyResult(\think\Model  $result, string  $relation, array  $subRelation = [], \Closure  $closure = null, array  $cache = [], bool  $join = false) : void

预载入关联查询(数据)

Parameters

\think\Model $result

数据对象

string $relation

当前关联名

array $subRelation

子关联名

\Closure $closure

闭包

array $cache

关联缓存

bool $join

是否为JOIN方式

save()

save(mixed  $data, bool  $replace = true) : \think\Model|false

保存(新增)当前关联数据对象

Parameters

mixed $data

数据 可以使用数组 关联模型对象

bool $replace

是否自动识别更新和写入

Returns

\think\Model|false —

bind()

bind(array  $attr) : $this

绑定关联表的属性到父模型属性

Parameters

array $attr

要绑定的属性列表

Returns

$this —

getBindAttr()

getBindAttr() : array

获取绑定属性

Returns

array —

getParent()

getParent() : \think\Model

获取关联的所属模型

Returns

\think\Model —

getQuery()

getQuery() : \think\db\BaseQuery

获取当前的关联模型类的Query实例

Returns

\think\db\BaseQuery —

getModel()

getModel() : \think\Model

获取当前的关联模型类的实例

Returns

\think\Model —

isSelfRelation()

isSelfRelation() : bool

当前关联是否为自关联

Returns

bool —

update()

update(array  $data = []) : int

更新数据

Parameters

array $data

更新数据

Returns

int —

delete()

delete(mixed  $data = null) : int

删除记录

Parameters

mixed $data

表达式 true 表示强制删除

Throws

\think\db\exception\DbException
\think\model\PDOException

Returns

int —

withLimit()

withLimit(int  $limit) : $this

限制关联数据的数量

Parameters

int $limit

关联数量限制

Returns

$this —

withField()

withField(array  $field) : $this

限制关联数据的字段

Parameters

array $field

关联字段限制

Returns

$this —

__call()

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

Parameters

mixed $method
mixed $args

Returns

mixed —

__construct()

__construct(\think\Model  $parent, string  $model, string  $foreignKey, string  $localKey, string  $relation = null) : mixed

架构函数

Parameters

\think\Model $parent

上级模型对象

string $model

模型名

string $foreignKey

关联外键

string $localKey

关联主键

string $relation

关联名

Returns

mixed —

getRelation()

getRelation(array  $subRelation = [], \Closure  $closure = null) : \think\Model

延迟获取关联数据

Parameters

array $subRelation

子关联名

\Closure $closure

闭包查询条件

Returns

\think\Model —

getRelationCountQuery()

getRelationCountQuery(\Closure  $closure = null, string  $aggregate = 'count', string  $field = '*', string  $name = '') : string

创建关联统计子查询

Parameters

\Closure $closure

闭包

string $aggregate

聚合查询方法

string $field

字段

string $name

聚合字段别名

Returns

string —

relationCount()

relationCount(\think\Model  $result, \Closure  $closure = null, string  $aggregate = 'count', string  $field = '*', string  $name = null) : int

关联统计

Parameters

\think\Model $result

数据对象

\Closure $closure

闭包

string $aggregate

聚合查询方法

string $field

字段

string $name

统计字段别名

Returns

int —

has()

has(string  $operator = '>=', int  $count = 1, string  $id = '*', string  $joinType = '', \think\db\BaseQuery  $query = null) : \think\db\BaseQuery

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

Parameters

string $operator

比较操作符

int $count

个数

string $id

关联表的统计字段

string $joinType

JOIN类型

\think\db\BaseQuery $query

Query对象

Returns

\think\db\BaseQuery —

hasWhere()

hasWhere(mixed  $where = [], mixed  $fields = null, string  $joinType = '', \think\db\BaseQuery  $query = null) : \think\db\BaseQuery

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

Parameters

mixed $where

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

mixed $fields

字段

string $joinType

JOIN类型

\think\db\BaseQuery $query

Query对象

Returns

\think\db\BaseQuery —

associate()

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

添加关联数据

Parameters

\think\Model $model

Returns

\think\Model —

dissociate()

dissociate() : \think\Model

注销关联数据

Returns

\think\Model —

eagerlySet()

eagerlySet(array  $resultSet, string  $relation, array  $subRelation = [], \Closure  $closure = null, array  $cache = []) : void

预载入关联查询(数据集)

Parameters

array $resultSet

数据集

string $relation

当前关联名

array $subRelation

子关联名

\Closure $closure

闭包

array $cache

关联缓存

eagerlyOne()

eagerlyOne(\think\Model  $result, string  $relation, array  $subRelation = [], \Closure  $closure = null, array  $cache = []) : void

预载入关联查询(数据)

Parameters

\think\Model $result

数据对象

string $relation

当前关联名

array $subRelation

子关联名

\Closure $closure

闭包

array $cache

关联缓存

match()

match(string  $model, string  $relation, \think\Model  $result) : void

一对一 关联模型预查询拼装

Parameters

string $model

模型名称

string $relation

关联名

\think\Model $result

模型对象实例

bindAttr()

bindAttr(\think\Model  $result, \think\Model  $model = null) : void

绑定关联属性到父模型

Parameters

\think\Model $result

父模型对象

\think\Model $model

关联模型对象

Throws

\think\db\exception\DbException

eagerlyWhere()

eagerlyWhere(array  $where, string  $key, array  $subRelation = [], \Closure  $closure = null, array  $cache = []) : array

一对一 关联模型预查询(IN方式)

Parameters

array $where

关联预查询条件

string $key

关联键名

array $subRelation

子关联

\Closure $closure
array $cache

关联缓存

Returns

array —

resultSetBuild()

resultSetBuild(array  $resultSet, \think\Model  $parent = null) : mixed

封装关联数据集

Parameters

array $resultSet

数据集

\think\Model $parent

父模型

Returns

mixed —

getQueryFields()

getQueryFields(string  $model) : mixed

Parameters

string $model

Returns

mixed —

getRelationQueryFields()

getRelationQueryFields(mixed  $fields, string  $model) : mixed

Parameters

mixed $fields
string $model

Returns

mixed —

getQueryWhere()

getQueryWhere(array  $where, string  $relation) : void

Parameters

array $where
string $relation

getClosureType()

getClosureType(\Closure  $closure) : mixed

判断闭包的参数类型

Parameters

\Closure $closure

Returns

mixed —

baseQuery()

baseQuery() : void

执行基础查询(仅执行一次)