trait Relation

实体关联处理

Methods

mixed
getOption(string $opt = null)

获取选项

static;
setOption(string $opt, mixed $value)

设置选项

writeWith(array $writeWithRelation)

关联数据写

static Entity
has(string $relation, mixed $operator = '>=', int $count = 1, array $where = [])

查询已存在的关联

static Entity
whereHas(string $relation, array|Closure $where = null, mixed $operator = '>=', int $count = 1)

带上条件查询已存在的关联(本方法执行的where会自动带上关联实体的表前缀)

static Entity
doesntHave(string $relation, mixed $operator = '>=', int $count = 1, array $where = [])

查询不存在的关联

static Entity
whereDoesntHave(string $relation, array|Closure $where = null, mixed $operator = '>=', int $count = 1)

带上条件查询不存在的关联(本方法执行的where会自动带上关联实体的表前缀)

bool
associatedPreloadWithJoin(Entity $entity, string $relation, mixed $subField = '*', string $joinType = '', Closure $closure = null, bool $addMainTable = false)

预载入关联查询 JOIN方式

void
collectAssociatedPreload(array $resultSet, array $relations, bool $join = false)

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

hasOne(string $entity, string $foreignKey = '', string $localKey = '')

HAS ONE 关联定义

belongsTo(string $entity, string $foreignKey = '', string $localKey = '')

BELONGS TO 关联定义

hasMany(string $entity, string $foreignKey = '', string $localKey = '')

HAS MANY 关联定义

hasManyThrough(string $entity, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = '')

HAS MANY 远程关联定义

hasOneThrough(string $entity, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = '')

HAS ONE 远程关联定义

belongsToMany(string $entity, string $middle = '', string $foreignKey = '', string $localKey = '')

BELONGS TO MANY 关联定义

morphOne(string $entity, string|array $morph = null, string $type = '')

MORPH One 关联定义

morphMany(string $entity, string|array $morph = null, string $type = '')

MORPH MANY 关联定义

morphTo(string|array $morph = null, array $alias = [])

MORPH TO 关联定义

string
parseEntity(string $entity)

解析实体的完整命名空间

string
getForeignKey(string $name)

获取实体的默认外键名

void
writeWithSave()

关联新增、写入 OneToOne

void
writeWithDelete()

关联删除 OneToOne、OneToMany

static Relation
with(array|string $with, Entity $entity = null)

关联预载入 In方式

static Relation
withJoin(array|string $with, Entity $entity = null, string $joinType = 'inner')

关联预载入 JOIN方式-只适用于HasOne和BelongsTo

static Relation
withAggregate(array|string $relations, string $aggregate = 'count', string $aggregateField = '*', mixed $columns = '*')

关联统计

static Relation
withCount(string|array $relation, string $aggregateField = '*', mixed $columns = '*')

关联统计

static Relation
withSum(string|array $relation, string $aggregateField, mixed $columns = '*')

关联统计Sum

static Relation
withMax(string|array $relation, string $aggregateField, mixed $columns = '*')

关联统计Max

static Relation
withMin(string|array $relation, string $aggregateField, mixed $columns = '*')

关联统计Min

static Relation
withAvg(string|array $relation, string $aggregateField, mixed $columns = '*')

关联统计Avg

relationAggregate(Entity $entity, array $relations, string $aggregate = 'sum', string $aggregateField = '*')

关联聚合

Details

at line 62
mixed getOption(string $opt = null)

获取选项

Parameters

string $opt

Return Value

mixed

at line 76
static; setOption(string $opt, mixed $value)

设置选项

Parameters

string $opt
mixed $value

Return Value

static;

at line 89
Relation writeWith(array $writeWithRelation)

关联数据写

Parameters

array $writeWithRelation 关联

Return Value

Relation

at line 114
static Entity has(string $relation, mixed $operator = '>=', int $count = 1, array $where = [])

查询已存在的关联

Parameters

string $relation 关联方法名
mixed $operator 比较操作符
int $count 个数
array $where | Closure $where (数组或者闭包)

Return Value

Entity

at line 137
static Entity whereHas(string $relation, array|Closure $where = null, mixed $operator = '>=', int $count = 1)

带上条件查询已存在的关联(本方法执行的where会自动带上关联实体的表前缀)

Parameters

string $relation 关联方法名
array|Closure $where (数组或者闭包) 查询条件 eg: UsersEntity::whereHas('logs',function(Entity $query) { $query->whereGt('ctime', Cml::$nowTime - 86400); //生成的where语句 LoginLogEntity.ctime > '1577354243' })->select(0, 2); UsersEntity::whereHas('logs',[ 'ctime' => '1575203774' //生成where语句 LoginLogEntity.ctime = '1575203774' ])->select(0, 2);
mixed $operator 比较操作符
int $count 个数

Return Value

Entity

at line 152
static Entity doesntHave(string $relation, mixed $operator = '>=', int $count = 1, array $where = [])

查询不存在的关联

Parameters

string $relation 关联方法名
mixed $operator 比较操作符
int $count 个数
array $where | Closure $where (数组或者闭包)

Return Value

Entity

at line 175
static Entity whereDoesntHave(string $relation, array|Closure $where = null, mixed $operator = '>=', int $count = 1)

带上条件查询不存在的关联(本方法执行的where会自动带上关联实体的表前缀)

Parameters

string $relation 关联方法名
array|Closure $where (数组或者闭包) 查询条件 eg: UsersEntity::whereHas('logs',function(Entity $query) { $query->whereGt('ctime', Cml::$nowTime - 86400); //生成的where语句 LoginLogEntity.ctime > '1577354243' })->select(0, 2); UsersEntity::whereHas('logs',[ 'ctime' => '1575203774' //生成where语句 LoginLogEntity.ctime = '1575203774' ])->select(0, 2);
mixed $operator 比较操作符
int $count 个数

Return Value

Entity

at line 192
bool associatedPreloadWithJoin(Entity $entity, string $relation, mixed $subField = '*', string $joinType = '', Closure $closure = null, bool $addMainTable = false)

预载入关联查询 JOIN方式

Parameters

Entity $entity
string $relation 关联方法名
mixed $subField 关联子表的字段
string $joinType JOIN类型
Closure $closure 闭包
bool $addMainTable 是否添加主表

Return Value

bool

at line 214
void collectAssociatedPreload(array $resultSet, array $relations, bool $join = false)

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

Parameters

array $resultSet 数据集
array $relations 关联名
bool $join 是否为JOIN方式

Return Value

void

at line 253
HasOne hasOne(string $entity, string $foreignKey = '', string $localKey = '')

HAS ONE 关联定义

Parameters

string $entity 实体名
string $foreignKey 关联外键
string $localKey 当前主键

Return Value

HasOne

at line 272
BelongsTo belongsTo(string $entity, string $foreignKey = '', string $localKey = '')

BELONGS TO 关联定义

Parameters

string $entity 实体名
string $foreignKey 关联外键
string $localKey 关联主键

Return Value

BelongsTo

at line 290
HasMany hasMany(string $entity, string $foreignKey = '', string $localKey = '')

HAS MANY 关联定义

Parameters

string $entity 实体名
string $foreignKey 关联外键
string $localKey 当前主键

Return Value

HasMany

at line 312
HasManyThrough hasManyThrough(string $entity, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = '')

HAS MANY 远程关联定义

Parameters

string $entity 实体名
string $through 中间实体名
string $foreignKey 外键
string $throughKey 中间表外键
string $localKey 当前主键
string $throughPk 中间表主键

Return Value

HasManyThrough

at line 338
HasOneThrough hasOneThrough(string $entity, string $through, string $foreignKey = '', string $throughKey = '', string $localKey = '', string $throughPk = '')

HAS ONE 远程关联定义

Parameters

string $entity 实体名
string $through 中间实体名
string $foreignKey 关联外键
string $throughKey 关联外键
string $localKey 当前主键
string $throughPk 中间表主键

Return Value

HasOneThrough

at line 362
BelongsToMany belongsToMany(string $entity, string $middle = '', string $foreignKey = '', string $localKey = '')

BELONGS TO MANY 关联定义

Parameters

string $entity 实体名
string $middle 中间表/实体名
string $foreignKey 关联外键
string $localKey 当前实体关联键

Return Value

BelongsToMany

at line 383
MorphOne morphOne(string $entity, string|array $morph = null, string $type = '')

MORPH One 关联定义

Parameters

string $entity 实体名
string|array $morph 多态字段信息
string $type 多态类型

Return Value

MorphOne

at line 407
MorphMany morphMany(string $entity, string|array $morph = null, string $type = '')

MORPH MANY 关联定义

Parameters

string $entity 实体名
string|array $morph 多态字段信息
string $type 当前实体对应的多态类型

Return Value

MorphMany

at line 431
MorphTo morphTo(string|array $morph = null, array $alias = [])

MORPH TO 关联定义

Parameters

string|array $morph 多态字段信息
array $alias 多态别名定义 如: ['user' => UserChangeEntity::class] 表里存的关联类型是user,如果不定义别名关联的是UserEntity::class

Return Value

MorphTo

at line 452
protected string parseEntity(string $entity)

解析实体的完整命名空间

Parameters

string $entity 实体名(或者完整类名)

Return Value

string

at line 471
string getForeignKey(string $name)

获取实体的默认外键名

Parameters

string $name 实体名

Return Value

string

at line 484
protected void writeWithSave()

关联新增、写入 OneToOne

Return Value

void

at line 501
protected void writeWithDelete()

关联删除 OneToOne、OneToMany

Return Value

void

at line 527
static Relation with(array|string $with, Entity $entity = null)

关联预载入 In方式

Parameters

array|string $with 关联方法名,如:'prifile'、['prifile', 'logs]、['profile' => 'id , title']只取profile的id,title字段、 ['profile'=> function(Entity $entity) {$entity->whereGt('id', 1);}]子条件 ['logs' => ['detail', 'detail2']] 嵌套查询 ['logs.detail'] 嵌套查询
Entity $entity 以实例方法调用时传入对象 可用来配置要查询的字段

Return Value

Relation

at line 542
static Relation withJoin(array|string $with, Entity $entity = null, string $joinType = 'inner')

关联预载入 JOIN方式-只适用于HasOne和BelongsTo

Parameters

array|string $with 关联方法名,如:'prifile'、['profile'=>'id , title']只取profile的id,title字段、['profile'=> function(Entity $entity) {$entity->whereGt('id', 1);}]子条件
Entity $entity 以实例方法调用时传入对象 可用来配置要查询的字段
string $joinType JOIN方式

Return Value

Relation

at line 578
static protected Relation withAggregate(array|string $relations, string $aggregate = 'count', string $aggregateField = '*', mixed $columns = '*')

关联统计

Parameters

array|string $relations 关联方法名
string $aggregate 聚合查询方法
string $aggregateField 字段
mixed $columns 要查询的主表字段 *

Return Value

Relation

at line 597
static Relation withCount(string|array $relation, string $aggregateField = '*', mixed $columns = '*')

关联统计

Parameters

string|array $relation 关联方法名
string $aggregateField 聚合的字段名
mixed $columns 要查询的主表字段 *

Return Value

Relation

at line 611
static Relation withSum(string|array $relation, string $aggregateField, mixed $columns = '*')

关联统计Sum

Parameters

string|array $relation 关联方法名
string $aggregateField 聚合的字段名
mixed $columns 要查询的主表字段 *

Return Value

Relation

at line 625
static Relation withMax(string|array $relation, string $aggregateField, mixed $columns = '*')

关联统计Max

Parameters

string|array $relation 关联方法名
string $aggregateField 聚合的字段名
mixed $columns 要查询的主表字段 *

Return Value

Relation

at line 639
static Relation withMin(string|array $relation, string $aggregateField, mixed $columns = '*')

关联统计Min

Parameters

string|array $relation 关联方法名
string $aggregateField 聚合的字段名
mixed $columns 要查询的主表字段 *

Return Value

Relation

at line 653
static Relation withAvg(string|array $relation, string $aggregateField, mixed $columns = '*')

关联统计Avg

Parameters

string|array $relation 关联方法名
string $aggregateField 聚合的字段名
mixed $columns 要查询的主表字段 *

Return Value

Relation

at line 668
Entity relationAggregate(Entity $entity, array $relations, string $aggregate = 'sum', string $aggregateField = '*')

关联聚合

Parameters

Entity $entity 实体
array $relations 关联名
string $aggregate 聚合查询方法
string $aggregateField 字段

Return Value

Entity