class Entity implements ArrayAccess, Arrayable, IteratorAggregate, Jsonable, JsonSerializable

数据库实体实现,快捷方法不可用

Traits

基础Model类,在CmlPHP中负责数据的存取(目前包含db/cache)
Trait OverrideDbMethod
实体关联处理

Properties

protected bool $useMaster 快捷方法-读是否强制使用主库 from Base
protected mixed $cacheExpire 查询数据缓存时间 from Base
protected null|string $tablePrefix 表前缀 from Base
protected string $db 数据库配置key from Base
protected null|string $table 表名 from Base
protected null $primaryKey 主键-不设置则自动从缓存获取 from Base
protected $pdoFetchStyle pdo获取数据的方式,默认返回数组 from Base
protected array $items 元素 from ArrayJsonSpl
protected bool|int $autoCreateTime 自动写入插入时间
protected bool|int $autoUpdateTime 是否自动写入更新时间

Methods

Db
db(string $conf = '')

获取db实例

from Base
$this
cloneSelf()

clone一个自身用于复杂条件

from Base
closeDb(string $conf = 'default_db')

当程序连接N个db的时候用于释放于用连接以节省内存

from Base
setCacheExpire(mixed $cacheExpire = null)

设置查询数据缓存时间

from Base
cache(string $conf = 'default_cache')

获取cache实例

from Base
static Base
getInstance(null|string $table = null, null|string $tablePrefix = null, null|string|array $db = null)

获取一个Model实例

from Base
string
getTableName(bool $addTablePrefix = false, bool $addDbName = false)

获取表名

from Base
string
getDbConf()

获取当前Model的数据库配置串

from Base
Db
mapDbAndTable() deprecated

兼容旧版本--已经不再需要

from Base
static Cache
staticCache(string $conf = 'default_cache')

静态方式获取cache实例

from Base
Base|mixed
__call($dbMethod, $arguments)

当访问model中不存在的方法时直接调用$this->db()的相关方法

from Base
static Base
__callStatic($dbMethod, $arguments)

当访问model中不存在的方法时直接调用相关model中的db()的相关方法

from Base
when(bool $condition, callable $trueCallback, callable $falseCallback = null)

根据条件是否成立执行对应的闭包

from Base
mixed
getPrimaryKey(string $tableName = null, mixed $tablePrefix = null)

获取主键

from Base
__clone()

No description

from Base
string|array
buildSql($offset = null, $limit = null, $isSelect = false)

构建sql

from Base
int
delete()

删除数据

from Base
int
count($field = '*', $isMulti = false, $useMaster = false)

获取 COUNT(字段名或*) 的结果

from Base
float
max($field = '*', $isMulti = false, $useMaster = false)

获取 MAX(字段名或*) 的结果

from Base
float
min($field = '*', $isMulti = false, $useMaster = false)

获取 MIN(字段名或*) 的结果

from Base
float
sum($field = '*', $isMulti = false, $useMaster = false)

获取 SUM(字段名或*) 的结果

from Base
float
avg($field = '*', $isMulti = false, $useMaster = false)

获取 AVG(字段名或*) 的结果

from Base
array
jsonSerialize(bool $transform = true)

将元素转换为可序列化JSON

bool
isEmpty()

判断集合是否为空

toJson(bool $transform = true, int $options = JSON_UNESCAPED_UNICODE)

No description

bool
offsetExists(mixed $key)

判断元素是否存在

mixed
offsetGet(mixed $key)

获取一个元素

offsetSet(mixed $key, mixed $value)

设置一个元素

offsetUnset(string $key)

Unset一个元素

array
toArray(bool $transform = true)

当前集合转换成数组返回

getIterator()

获取元素的迭代器

__toString()

No description

int
insert(array $data)

增加一条数据 entity不暴露此方法,使用save或create

int
update($data)

更新数据 entity不暴露此方法,使用save

bool
insertMulti(array $field, array $data, bool $openTransAction = true)

增加多条数据

int
replaceInto(array $data)

插入或替换一条记录 若AUTO_INCREMENT存在则返回 AUTO_INCREMENT 的值.

int
upSet(array $data, array $up = [])

插入或更新一条记录,当UNIQUE index or PRIMARY KEY存在的时候更新,不存在的时候插入 若AUTO_INCREMENT存在则返回 AUTO_INCREMENT 的值.

bool
replaceMulti(array $field, array $data, string $tableName = null, mixed $tablePrefix = null, bool $openTransAction = true)

插入或替换多条记录

forceIndex(string $index)

强制使用索引

mixed
getOption(string $opt = null)

获取选项

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

设置选项

from Relation
writeWith(array $writeWithRelation)

关联数据写

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

查询已存在的关联

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

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

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

查询不存在的关联

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

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

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

预载入关联查询 JOIN方式

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

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

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

HAS ONE 关联定义

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

BELONGS TO 关联定义

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

HAS MANY 关联定义

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

HAS MANY 远程关联定义

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

HAS ONE 远程关联定义

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

BELONGS TO MANY 关联定义

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

MORPH One 关联定义

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

MORPH MANY 关联定义

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

MORPH TO 关联定义

from Relation
string
parseEntity(string $entity)

解析实体的完整命名空间

from Relation
string
getForeignKey(string $name)

获取实体的默认外键名

from Relation
void
writeWithSave()

关联新增、写入 OneToOne

from Relation
void
writeWithDelete()

关联删除 OneToOne、OneToMany

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

关联预载入 In方式

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

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

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

关联统计

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

关联统计

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

关联统计Sum

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

关联统计Max

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

关联统计Min

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

关联统计Avg

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

关联聚合

from Relation
select(int $offset = null, int $limit = null, bool $useMaster = false, mixed $fieldAsKey = false)

获取多条数据

paginate(int $limit, bool $useMaster = false, null|int $page = null, mixed $fieldAsKey = false)

分页获取数据

getOne(bool $useMaster = false)

获取一条数据

__construct(array $items = [])

实体查询join相关语句通过配置关联关系来操作

static static;
make(array $items = [])

返回一个全新的实例

mixed
__get(string $field)

获取某个字段的值

__set(string $field, mixed $value)

更新字段

setItem(string $field, mixed $value)

设置一个值-不触发更新

bool
__isset(string $field)

检测数据对象的值

void
__unset(string $field)

销毁数据对象的值

static bool
destroy(mixed $val, mixed $field = null)

根据主键删除

static Entity
find(int $val = null, mixed $field = null)

根据主键获取数据

static Entity[]|Collection
findMany(int $offset = null, int $limit = null, bool $useMaster = false, mixed $fieldAsKey = false)

获取多条数据

static Generator
cursor(int $offset = null, int $limit = null, bool $useMaster = false)

获取给定查询的生成器

static Entity
findOrFail(int $val = null, mixed $field = null)

根据主键获取数据-不存在则抛出异常

itemIsExists(bool $isUpdate = true, bool $isRead = false)

save操作是否为update

int
save(array $item = [], bool $startTransAction = true)

保存数据

saveMulti(array $entityArray)

保存数据-多条

static Entity
create(array $item)

新增

fill(array $items)

填充

mixed
remove()

删除

reset(array $items = [])

重置

array
__debugInfo()

__debugInfo

$this
withTimestamps(string $createTimeField = 'ctime', string $updateTimeField = 'utime')

自动维护时间戳

Details

in Base at line 107
Db db(string $conf = '')

获取db实例

Parameters

string $conf 使用的数据库配置;

Return Value

Db | DbBase

in Base at line 136
$this cloneSelf()

clone一个自身用于复杂条件

Return Value

$this

in Base at line 146
closeDb(string $conf = 'default_db')

当程序连接N个db的时候用于释放于用连接以节省内存

Parameters

string $conf 使用的数据库配置;

in Base at line 163
Base setCacheExpire(mixed $cacheExpire = null)

设置查询数据缓存时间

表数据有变动会自动更新缓存。设置为0表示表数据没变动时缓存不过期。 这边设置为3600意思是即使表数据没变动也让缓存每3600s失效一次,这样可以让缓存空间更合理的利用. 如果不想启用缓存直接配置为false 默认为null: 使用 db配置中的cache_expire

Parameters

mixed $cacheExpire

Return Value

Base

in Base at line 177
Cache cache(string $conf = 'default_cache')

获取cache实例

Parameters

string $conf 使用的缓存配置;

Return Value

Cache

in Base at line 203
static Base getInstance(null|string $table = null, null|string $tablePrefix = null, null|string|array $db = null)

获取一个Model实例

Parameters

null|string $table 表名
null|string $tablePrefix 表前缀
null|string|array $db db配置,默认default_db

Return Value

Base

in Base at line 225
string getTableName(bool $addTablePrefix = false, bool $addDbName = false)

获取表名

Parameters

bool $addTablePrefix 是否返回带表前缀的完整表名
bool $addDbName 是否带上dbname

Return Value

string

in Base at line 247
string getDbConf()

获取当前Model的数据库配置串

Return Value

string

in Base at line 258
Db mapDbAndTable() deprecated

deprecated

兼容旧版本--已经不再需要

Return Value

Db

in Base at line 270
static Cache staticCache(string $conf = 'default_cache')

静态方式获取cache实例

Parameters

string $conf 使用的缓存配置;

Return Value

Cache

in Base at line 283
Base|mixed __call($dbMethod, $arguments)

当访问model中不存在的方法时直接调用$this->db()的相关方法

Parameters

$dbMethod
$arguments

Return Value

Base|mixed

in Base at line 302
static Base __callStatic($dbMethod, $arguments)

当访问model中不存在的方法时直接调用相关model中的db()的相关方法

Parameters

$dbMethod
$arguments

Return Value

Base

in Base at line 324
Base when(bool $condition, callable $trueCallback, callable $falseCallback = null)

根据条件是否成立执行对应的闭包

Parameters

bool $condition 条件
callable $trueCallback 条件成立执行的闭包
callable $falseCallback 条件不成立执行的闭包

Return Value

Base

in Base at line 342
mixed getPrimaryKey(string $tableName = null, mixed $tablePrefix = null)

获取主键

Parameters

string $tableName 表名 不传会自动从当前Model中$table属性获取
mixed $tablePrefix 表前缀 不传会自动从当前Model中$tablePrefix属性获取再没有则获取配置中配置的前缀

Return Value

mixed

in Base at line 349
__clone()

in Base at line 32
string|array buildSql($offset = null, $limit = null, $isSelect = false)

构建sql

Parameters

$offset
$limit
$isSelect

Return Value

string|array

in Base at line 32
int delete()

删除数据

Return Value

int

in Base at line 32
int count($field = '*', $isMulti = false, $useMaster = false)

获取 COUNT(字段名或*) 的结果

Parameters

$field
$isMulti
$useMaster

Return Value

int

in Base at line 32
float max($field = '*', $isMulti = false, $useMaster = false)

获取 MAX(字段名或*) 的结果

Parameters

$field
$isMulti
$useMaster

Return Value

float

in Base at line 32
float min($field = '*', $isMulti = false, $useMaster = false)

获取 MIN(字段名或*) 的结果

Parameters

$field
$isMulti
$useMaster

Return Value

float

in Base at line 32
float sum($field = '*', $isMulti = false, $useMaster = false)

获取 SUM(字段名或*) 的结果

Parameters

$field
$isMulti
$useMaster

Return Value

float

in Base at line 32
float avg($field = '*', $isMulti = false, $useMaster = false)

获取 AVG(字段名或*) 的结果

Parameters

$field
$isMulti
$useMaster

Return Value

float

at line 565
array jsonSerialize(bool $transform = true)

将元素转换为可序列化JSON

Parameters

bool $transform 是否自动使用获取器转换

Return Value

array

in ArrayJsonSpl at line 52
bool isEmpty()

判断集合是否为空

Return Value

bool

at line 594
toJson(bool $transform = true, int $options = JSON_UNESCAPED_UNICODE)

Parameters

bool $transform 是否自动使用获取器转换
int $options

in ArrayJsonSpl at line 76
bool offsetExists(mixed $key)

判断元素是否存在

Parameters

mixed $key

Return Value

bool

at line 606
mixed offsetGet(mixed $key)

获取一个元素

Parameters

mixed $key

Return Value

mixed

at line 617
offsetSet(mixed $key, mixed $value)

设置一个元素

Parameters

mixed $key
mixed $value

in ArrayJsonSpl at line 113
offsetUnset(string $key)

Unset一个元素

Parameters

string $key

at line 546
array toArray(bool $transform = true)

当前集合转换成数组返回

Parameters

bool $transform 是否自动使用获取器转换

Return Value

array

in ArrayJsonSpl at line 135
ArrayIterator getIterator()

获取元素的迭代器

Return Value

ArrayIterator

in ArrayJsonSpl at line 140
__toString()

in OverrideDbMethod at line 28
protected int insert(array $data)

增加一条数据 entity不暴露此方法,使用save或create

Parameters

array $data 要新增的数据

Return Value

int

in OverrideDbMethod at line 40
protected int update($data)

更新数据 entity不暴露此方法,使用save

Parameters

$data

Return Value

int

in OverrideDbMethod at line 54
bool insertMulti(array $field, array $data, bool $openTransAction = true)

增加多条数据

Parameters

array $field 要插入的字段 eg: ['title', 'msg', 'status', 'ctime’]
array $data 多条数据的值 eg: [['标题1', '内容1', 1, '2017'], ['标题2', '内容2', 1, '2017']]
bool $openTransAction 是否开启事务 默认开启

Return Value

bool | array

in OverrideDbMethod at line 67
int replaceInto(array $data)

插入或替换一条记录 若AUTO_INCREMENT存在则返回 AUTO_INCREMENT 的值.

Parameters

array $data 插入/更新的值 eg: ['username'=>'admin', 'email'=>'linhechengbush@live.com']

Return Value

int

in OverrideDbMethod at line 81
int upSet(array $data, array $up = [])

插入或更新一条记录,当UNIQUE index or PRIMARY KEY存在的时候更新,不存在的时候插入 若AUTO_INCREMENT存在则返回 AUTO_INCREMENT 的值.

Parameters

array $data 插入的值 eg: ['username'=>'admin', 'email'=>'linhechengbush@live.com']
array $up 更新的值-会自动merge $data中的数据

Return Value

int

in OverrideDbMethod at line 97
bool replaceMulti(array $field, array $data, string $tableName = null, mixed $tablePrefix = null, bool $openTransAction = true)

插入或替换多条记录

Parameters

array $field 要插入的字段 eg: ['title', 'msg', 'status', 'ctime’]
array $data 多条数据的值 eg: [['标题1', '内容1', 1, '2017'], ['标题2', '内容2', 1, '2017']]
string $tableName 表名 不传会自动从当前Model中$table属性获取
mixed $tablePrefix 表前缀 不传会自动从当前Model中$tablePrefix属性获取再没有则获取配置中配置的前缀
bool $openTransAction 是否开启事务 默认开启

Return Value

bool | array

in OverrideDbMethod at line 111
OverrideDbMethod forceIndex(string $index)

强制使用索引

Parameters

string $index 要强制使用的索引

Return Value

OverrideDbMethod

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

获取选项

Parameters

string $opt

Return Value

mixed

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

设置选项

Parameters

string $opt
mixed $value

Return Value

static;

in Relation at line 89
Relation writeWith(array $writeWithRelation)

关联数据写

Parameters

array $writeWithRelation 关联

Return Value

Relation

in 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

in Relation 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

in Relation 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

in Relation 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

in Relation 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

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

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

Parameters

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

Return Value

void

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

HAS ONE 关联定义

Parameters

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

Return Value

HasOne

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

BELONGS TO 关联定义

Parameters

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

Return Value

BelongsTo

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

HAS MANY 关联定义

Parameters

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

Return Value

HasMany

in Relation 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

in Relation 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

in Relation 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

in Relation 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

in Relation 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

in Relation 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

in Relation at line 452
protected string parseEntity(string $entity)

解析实体的完整命名空间

Parameters

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

Return Value

string

in Relation at line 471
string getForeignKey(string $name)

获取实体的默认外键名

Parameters

string $name 实体名

Return Value

string

in Relation at line 484
protected void writeWithSave()

关联新增、写入 OneToOne

Return Value

void

in Relation at line 501
protected void writeWithDelete()

关联删除 OneToOne、OneToMany

Return Value

void

in Relation 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

in 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

in 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

in 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

in 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

in 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

in 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

in 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

in 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

at line 86
Entity[]|Collection select(int $offset = null, int $limit = null, bool $useMaster = false, mixed $fieldAsKey = false)

获取多条数据

Parameters

int $offset 偏移量
int $limit 返回的条数
bool $useMaster 是否使用主库 默认读取从库
mixed $fieldAsKey 返回以某个字段做为key的数组

Return Value

Entity[]|Collection

at line 101
Entity[]|Collection paginate(int $limit, bool $useMaster = false, null|int $page = null, mixed $fieldAsKey = false)

分页获取数据

Parameters

int $limit 每页返回的条数
bool $useMaster 是否使用主库 默认读取从库
null|int $page 当前页数-不传则获取配置中var_page配置的request值
mixed $fieldAsKey 返回以某个字段做为key的数组

Return Value

Entity[]|Collection

at line 113
Entity getOne(bool $useMaster = false)

获取一条数据

Parameters

bool $useMaster 是否使用主库 默认读取从库

Return Value

Entity | bool

at line 125
__construct(array $items = [])

实体查询join相关语句通过配置关联关系来操作

Parameters

array $items 初始化赋值

Entity constructor.

at line 137
static static; make(array $items = [])

返回一个全新的实例

Parameters

array $items

Return Value

static;

at line 149
mixed __get(string $field)

获取某个字段的值

Parameters

string $field 字段

Return Value

mixed

at line 197
__set(string $field, mixed $value)

更新字段

Parameters

string $field 字段
mixed $value

at line 214
Entity setItem(string $field, mixed $value)

设置一个值-不触发更新

Parameters

string $field
mixed $value

Return Value

Entity

at line 227
bool __isset(string $field)

检测数据对象的值

Parameters

string $field 字段

Return Value

bool

at line 239
void __unset(string $field)

销毁数据对象的值

Parameters

string $field 字段

Return Value

void

at line 252
static bool destroy(mixed $val, mixed $field = null)

根据主键删除

Parameters

mixed $val 要删除的值
mixed $field 条件字段-不传自动获取主键

Return Value

bool

at line 271
static Entity find(int $val = null, mixed $field = null)

根据主键获取数据

Parameters

int $val
mixed $field 条件字段-不传自动获取主键

Return Value

Entity

at line 298
static Entity[]|Collection findMany(int $offset = null, int $limit = null, bool $useMaster = false, mixed $fieldAsKey = false)

获取多条数据

Parameters

int $offset 偏移量
int $limit 返回的条数
bool $useMaster 是否使用主库 默认读取从库
mixed $fieldAsKey 返回以某个字段做为key的数组

Return Value

Entity[]|Collection

at line 312
static Generator cursor(int $offset = null, int $limit = null, bool $useMaster = false)

获取给定查询的生成器

Parameters

int $offset 偏移量
int $limit 返回的条数
bool $useMaster 是否使用主库 默认读取从库

Return Value

Generator

at line 328
static Entity findOrFail(int $val = null, mixed $field = null)

根据主键获取数据-不存在则抛出异常

Parameters

int $val
mixed $field 条件字段-不传自动获取主键

Return Value

Entity

Exceptions

EntityNotFoundException

at line 345
Entity itemIsExists(bool $isUpdate = true, bool $isRead = false)

save操作是否为update

Parameters

bool $isUpdate true为update false为insert
bool $isRead 是否为数据库读取回来

Return Value

Entity

at line 362
int save(array $item = [], bool $startTransAction = true)

保存数据

Parameters

array $item 数据
bool $startTransAction 是否使用事务

更新时,配合::where()->save([])使用,新增直接用create方法

Return Value

int 返回新增的id/更新的条数

at line 406
Collection saveMulti(array $entityArray)

保存数据-多条

Parameters

array $entityArray 集合数组

Return Value

Collection

at line 429
static Entity create(array $item)

新增

Parameters

array $item

Return Value

Entity

at line 443
Entity fill(array $items)

填充

Parameters

array $items

Return Value

Entity

at line 457
mixed remove()

删除

Return Value

mixed

at line 488
Entity reset(array $items = [])

重置

Parameters

array $items 初始化赋值

Return Value

Entity

at line 502
array __debugInfo()

__debugInfo

Return Value

array

at line 515
$this withTimestamps(string $createTimeField = 'ctime', string $updateTimeField = 'utime')

自动维护时间戳

Parameters

string $createTimeField
string $updateTimeField

Return Value

$this