类文档
表示存储在 Elastic Search 索引中的文档
属性摘要
-
$_accessible protected
array<string, bool>
此实体中可以安全地进行批量赋值的字段映射,每个字段名称都指向一个布尔值,表示其状态。空数组表示没有字段可以进行批量赋值。
-
$_accessors protected static
array<string, array<string, array<string, string>>>
保存每个类的获取器/设置器的缓存列表
-
$_dirty protected
array<string, bool>
保存此对象最初创建后修改或添加的字段列表。
-
$_errors protected
array<string, mixed>
此对象中存储的每个字段的错误列表。
-
$_fields protected
array<string, mixed>
保存此实体的所有字段及其值。
-
$_hasBeenVisited protected
bool
在递归遍历获取错误的实体时存储当前访问状态。
-
$_hidden protected
list<string>
应该不包含在此实体的 JSON 或数组表示中的字段名称列表。
-
$_invalid protected
array<string, mixed>
验证/修补时错误的无效字段及其数据的列表。
-
$_new protected
bool
指示此实体是否尚未持久化。实体默认假设它们是新的。您可以使用 Table::persisted() 根据数据库中的记录设置实体上的 new 标志。
-
$_original protected
array<string, mixed>
保存此实体中所有已更改的字段及其原始值。
-
$_originalFields protected
list<string>
保存最初在实例化时设置的所有字段,或在标记为干净后设置的所有字段
-
$_registryAlias protected
string
此实体来自的存储库的别名
-
$_result protected
Elastica\Result
保存从搜索查询中传递到构造函数的 Result 对象实例。它可能包含有关此文档的搜索操作的额外信息,例如突出显示、分数和版本。
-
$_virtual protected
list<string>
应该包含在此实体的 JSON 或数组表示中的计算或虚拟字段列表。如果一个字段同时存在于 _hidden 和 _virtual 中,则该字段不会出现在实体的数组/JSON 版本中。
-
$id public @property
mixed
常用主键的别名。
-
$requireFieldPresence protected
bool
访问属性时是否检查字段的存在。
-
$version public
int|string
方法摘要
-
__construct() public
接受来自搜索的数组或 Result 对象并构造一个代表 Elastic Search 索引中实体的文档。
-
__debugInfo() public
返回一个数组,该数组可用于描述此对象的内部状态。
-
__get() public
魔术获取器,用于访问已在此实体中设置的字段
-
__isset() public
返回此实体是否包含名为 $field 的字段且未设置为 null。
-
__set() public
魔术设置器,用于在此实体中添加或编辑字段
-
__toString() public
以人类可读格式返回此对象的字符串表示形式。
-
__unset() public
从此实体中删除字段
-
_accessor() protected static
获取获取器方法名称 获取器方法(可用或不可用)缓存在 $_accessors 中
-
_nestedErrors() protected
辅助方法,用于获取嵌套实体中的错误
-
_readError() protected
从一个或多个对象中读取错误。
-
_readHasErrors() protected
读取一个或多个对象是否有错误。
-
clean() public
将整个实体设置为干净,这意味着它将显示为没有修改或添加的字段。对于初始对象水合来说,这是一个有用的调用
-
explanation() public
返回此文档的解释数组,该数组来自 Elasticsearch。
-
extract() public
返回一个包含存储在此实体中的请求字段的数组,按字段名称索引
-
extractOriginal() public
返回一个包含存储在此实体中的请求原始字段的数组,按字段名称索引,如果存在。
-
extractOriginalChanged() public
仅返回一个数组,其中包含存储在此实体中的原始字段,按字段名称索引,如果存在。
-
get() public
按名称返回字段的值
-
getAccessible() public
返回此实体的原始访问配置。
*
通配符指的是所有字段。 -
getDirty() public
获取脏字段。
-
getError() public
返回字段的验证错误
-
getErrors() public
返回所有验证错误。
-
getHidden() public
获取隐藏字段。
-
getInvalid() public
获取无效字段及其数据的列表,用于验证/修补时的错误
-
getInvalidField() public
获取无效字段的单个值。如果未设置,则返回 null。
-
getOriginal() public
按名称返回原始字段的值
-
getOriginalFields() public
返回一个原始字段的数组。原始字段是实体初始化时拥有的字段。
-
getOriginalValues() public
获取实体的所有原始值。
-
getSource() public
返回此实体来自的存储库的别名。
-
getVirtual() public
获取此实体上的虚拟字段。
-
getVisible() public
获取可见字段列表。
-
has() public
返回此实体是否包含名为 $field 的字段。
-
hasErrors() public
返回此实体是否包含错误。
-
hasOriginal() public
返回字段是否具有原始值
-
hasValue() public
检查字段是否有值。
-
highlights() public
返回文档的突出显示数组,该数组来自 Elasticsearch,用于执行的查询。
-
index() public
返回此文档来自的 Elasticsearch 索引名称。
-
isAccessible() public
检查字段是否可访问
-
isDirty() public
检查实体是否为脏,或者其单个字段是否为脏。
-
isEmpty() public
检查字段是否为空
-
isNew() public
返回此实体是否已持久化。
-
isOriginalField() public
判断字段是否为原始字段
-
jsonSerialize() public
返回将被序列化为 JSON 的字段
-
offsetExists() public
实现 isset($entity);
-
offsetGet() public
实现 $entity[$offset];
-
offsetSet() public
实现 $entity[$offset] = $value;
-
offsetUnset() public
实现 unset($result[$offset]);
-
requireFieldPresence() public
启用/禁用访问属性时检查字段是否存在。
-
set() public
设置实体内的单个字段。
-
setAccess() public
存储字段值是否可以在此实体中更改或设置。 特殊字段
*
也可以标记为可访问或受保护,这意味着之前指定的任何其他字段都将获取其值。 例如,$entity->setAccess('*', true)
表示默认情况下,任何尚未指定的字段都将可访问。 -
setDirty() public
设置单个字段的脏状态。
-
setError() public
设置单个字段的错误
-
setErrors() public
将错误消息设置为实体
-
setHidden() public
设置隐藏字段。
-
setInvalid() public
将字段设置为无效,不可修补到实体中。
-
setInvalidField() public
将字段设置为无效,不可修补到实体中。
-
setNew() public
设置此实体的状态。
-
setOriginalField() protected
将给定的字段或字段列表设置为原始字段。 通常无需手动调用此方法。
-
setSource() public
设置源别名
-
setVirtual() public
设置此实体上的虚拟字段。
-
toArray() public
返回一个包含已设置为此实体的所有字段的数组
-
unset() public
从此实体中删除字段或字段列表
-
version() public
返回 Elasticsearch 返回的此文档的版本号
方法详情
__construct() ¶ public
__construct(Elastica\Result|array $data = [], array $options = [])
接受来自搜索的数组或 Result 对象并构造一个代表 Elastic Search 索引中实体的文档。
参数
-
Elastica\Result|array
$data optional 表示 Elasticsearch 文档的数组或 Result 对象
-
array
$options optional 一组用于设置文档状态的选项
__debugInfo() ¶ public
__debugInfo(): array<string, mixed>
返回一个数组,该数组可用于描述此对象的内部状态。
返回
array<string, mixed>
__get() ¶ public
__get(string $field): mixed
魔术获取器,用于访问已在此实体中设置的字段
参数
-
string
$field 要访问的字段的名称
返回
mixed
__isset() ¶ public
__isset(string $field): bool
返回此实体是否包含名为 $field 的字段且未设置为 null。
参数
-
string
$field 要检查的字段。
返回
bool
__set() ¶ public
__set(string $field, mixed $value): void
魔术设置器,用于在此实体中添加或编辑字段
参数
-
string
$field 要设置的字段的名称
-
mixed
$value 要设置为字段的值
返回
void
_accessor() ¶ protected static
_accessor(string $property, string $type): string
获取获取器方法名称 获取器方法(可用或不可用)缓存在 $_accessors 中
参数
-
string
$property 要从其派生 getter 名称的字段名称
-
string
$type 访问器类型('get' 或 'set')
返回
string
_nestedErrors() ¶ protected
_nestedErrors(string $field): array
辅助方法,用于获取嵌套实体中的错误
参数
-
string
$field 此实体中要检查错误的字段
返回
array
_readError() ¶ protected
_readError(Cake\Datasource\EntityInterface|iterable $object, string|null $path = null): array
从一个或多个对象中读取错误。
参数
-
Cake\Datasource\EntityInterface|iterable
$object 要从中读取错误的对象。
-
string|null
$path optional 错误的字段名称。
返回
array
_readHasErrors() ¶ protected
_readHasErrors(Cake\Datasource\EntityInterface|array $object): bool
读取一个或多个对象是否有错误。
参数
-
Cake\Datasource\EntityInterface|array
$object 要从中读取错误的对象。
返回
bool
explanation() ¶ public
explanation(): array
返回此文档的解释数组,该数组来自 Elasticsearch。
如果这是一个新文档,或者用于创建它的查询没有请求解释,则此函数将返回一个空数组。
返回
array
extract() ¶ public
extract(list<string> $fields, bool $onlyDirty = false): array<string, mixed>
返回一个包含存储在此实体中的请求字段的数组,按字段名称索引
参数
-
list<string>
$fields 要返回的字段列表
-
bool
$onlyDirty optional 仅当字段为脏状态时返回请求的字段
返回
array<string, mixed>
extractOriginal() ¶ public
extractOriginal(list<string> $fields): array<string, mixed>
返回一个包含存储在此实体中的请求原始字段的数组,按字段名称索引,如果存在。
与原始值不变的字段将包含在此方法的返回值中。
参数
-
list<string>
$fields 要返回的字段列表
返回
array<string, mixed>
extractOriginalChanged() ¶ public
extractOriginalChanged(list<string> $fields): array<string, mixed>
仅返回一个数组,其中包含存储在此实体中的原始字段,按字段名称索引,如果存在。
此方法将仅返回实体构建后已被修改的字段。 不变的字段将被省略。
参数
-
list<string>
$fields 要返回的字段列表
返回
array<string, mixed>
get() ¶ public
get(string $field): mixed
按名称返回字段的值
参数
-
string
$field 要检索的字段的名称
返回
mixed
抛出
InvalidArgumentException
如果传递了一个空的字段名称
getInvalid() ¶ public
getInvalid(): array<string, mixed>
获取无效字段及其数据的列表,用于验证/修补时的错误
返回
array<string, mixed>
getInvalidField() ¶ public
getInvalidField(string $field): mixed|null
获取无效字段的单个值。如果未设置,则返回 null。
参数
-
string
$field 字段的名称。
返回
mixed|null
getOriginal() ¶ public
getOriginal(string $field, bool $allowFallback = true): mixed
按名称返回原始字段的值
参数
-
string
$field 要检索原始值的字段的名称。
-
bool
$allowFallback optional 是否允许回退到当前字段值(如果不存在原始值)
返回
mixed
抛出
InvalidArgumentException
如果传递了一个空的字段名称。
getOriginalFields() ¶ public
getOriginalFields(): list<string>
返回一个原始字段的数组。原始字段是实体初始化时拥有的字段。
返回
list<string>
getVisible() ¶ public
getVisible(): list<string>
获取可见字段列表。
可见字段列表是所有标准字段加上虚拟字段减去隐藏字段。
返回
list<string>
has() ¶ public
has(list<string>|string $field): bool
返回此实体是否包含名为 $field 的字段。
即使字段设置为 null
,它也会返回 true
。
示例
$entity = new Entity(['id' => 1, 'name' => null]);
$entity->has('id'); // true
$entity->has('name'); // true
$entity->has('last_name'); // false
您可以通过传递一个数组来检查多个字段
$entity->has(['name', 'last_name']);
检查多个字段时,所有字段都必须具有一个值(即使是 null
)存在,该方法才能返回 true
。
参数
-
list<string>|string
$field 要检查的字段或字段。
返回
bool
hasErrors() ¶ public
hasErrors(bool $includeNested = true): bool
返回此实体是否包含错误。
参数
-
bool
$includeNested optional true 将检查嵌套实体是否有 hasErrors()
返回
bool
hasValue() ¶ public
hasValue(string $field): bool
检查字段是否有值。
此方法将对以下情况返回 true
- 非空字符串
- 非空数组
- 任何对象
- 整数,即使是
0
- 浮点数,即使是 0.0
以及其他所有情况下的 false。
参数
-
string
$field 要检查的字段。
返回
bool
highlights() ¶ public
highlights(): array
返回文档的突出显示数组,该数组来自 Elasticsearch,用于执行的查询。
如果这是一个新文档,或者用于创建它的查询没有请求高亮显示,则此函数将返回一个空数组。
返回
array
index() ¶ public
index(): string|null
返回此文档来自的 Elasticsearch 索引名称。
如果这是一个新文档,则此函数返回 null
返回
string|null
isAccessible() ¶ public
isAccessible(string $field): bool
检查字段是否可访问
示例
$entity->isAccessible('id'); // Returns whether it can be set or not
参数
-
string
$field 要检查的字段名称
返回
bool
isDirty() ¶ public
isDirty(string|null $field = null): bool
检查实体是否为脏,或者其单个字段是否为脏。
参数
-
string|null
$field optional 要检查状态的字段。 整个实体为空。
返回
bool
isEmpty() ¶ public
isEmpty(string $field): bool
检查字段是否为空
这与 PHP empty()
函数的工作方式不同。 如果是以下情况,该方法将返回 true
''
(空字符串)null
[]
以及其他所有情况下的 false。
参数
-
string
$field 要检查的字段。
返回
bool
jsonSerialize() ¶ public
jsonSerialize(): array<string, mixed>
返回将被序列化为 JSON 的字段
返回
array<string, mixed>
offsetExists() ¶ public
offsetExists(string $offset): bool
实现 isset($entity);
参数
-
string
$offset 要检查的偏移量。
返回
bool
offsetGet() ¶ public
offsetGet(string $offset): mixed
实现 $entity[$offset];
参数
-
string
$offset 要获取的偏移量。
返回
mixed
offsetSet() ¶ public
offsetSet(string $offset, mixed $value): void
实现 $entity[$offset] = $value;
参数
-
string
$offset 要设置的偏移量。
-
mixed
$value 要设置的值。
返回
void
offsetUnset() ¶ public
offsetUnset(string $offset): void
实现 unset($result[$offset]);
参数
-
string
$offset 要移除的偏移量。
返回
void
requireFieldPresence() ¶ public
requireFieldPresence(bool $value = true): void
启用/禁用访问属性时检查字段是否存在。
如果启用,在尝试访问不存在的属性时将抛出异常。
参数
-
bool
$value 可选 true
表示启用,false
表示禁用。
返回
void
set() ¶ public
set(array<string, mixed>|string $field, mixed $value = null, array<string, mixed> $options = []): $this
设置实体内的单个字段。
示例
$entity->set('name', 'Andrew');
还可以通过传递一个以字段 => 值对形式的哈希数组作为字段,在一个调用中为该实体批量赋值多个字段。
示例
$entity->set(['name' => 'andrew', 'id' => 1]);
echo $entity->name // prints andrew
echo $entity->id // prints 1
有时在为字段赋值时,绕过该实体中的 setter 函数非常方便。可以通过使用 $options
参数禁用 setter
选项来实现。
$entity->set('name', 'Andrew', ['setter' => false]);
$entity->set(['name' => 'Andrew', 'id' => 1], ['setter' => false]);
在接受用户输入时,应谨慎对待批量赋值,默认情况下,实体将在批量赋值字段时保护所有字段。可以使用 guard
选项为单个 set 调用禁用保护。
$entity->set(['name' => 'Andrew', 'id' => 1], ['guard' => false]);
单独赋值字段时,不需要使用 guard 选项。
// No need to use the guard option.
$entity->set('name', 'Andrew');
可以使用 asOriginal
选项将给定字段设置为原始字段,如果它在实例化实体时不存在。
$entity = new Entity(['name' => 'andrew', 'id' => 1]);
$entity->set('phone_number', '555-0134');
print_r($entity->getOriginalFields()) // prints ['name', 'id']
$entity->set('phone_number', '555-0134', ['asOriginal' => true]);
print_r($entity->getOriginalFields()) // prints ['name', 'id', 'phone_number']
参数
-
array<string, mixed>|string
$field 要设置的字段名称或带有其对应值的字段列表。
-
mixed
$value 可选 要设置为字段的值,如果第一个参数也是数组,则为数组,在这种情况下将被视为
$options
。-
array<string, mixed>
$options 可选 用于设置字段的选项。允许的选项键为
setter
、guard
和asOriginal
。
返回
$this
抛出
InvalidArgumentException
setAccess() ¶ public
setAccess(list<string>|string $field, bool $set): $this
存储字段值是否可以在此实体中更改或设置。 特殊字段 *
也可以标记为可访问或受保护,这意味着之前指定的任何其他字段都将获取其值。 例如,$entity->setAccess('*', true)
表示默认情况下,任何尚未指定的字段都将可访问。
也可以使用字段数组调用此方法,在这种情况下,它们将分别采用第二个参数中指定的访问权限值。
示例
$entity->setAccess('id', true); // Mark id as not protected
$entity->setAccess('author_id', false); // Mark author_id as protected
$entity->setAccess(['id', 'user_id'], true); // Mark both fields as accessible
$entity->setAccess('*', false); // Mark all fields as protected
参数
-
list<string>|string
$field 要更改其访问权限的单个或多个字段。
-
bool
$set True 表示该字段可访问,false 表示该字段受保护。
返回
$this
setDirty() ¶ public
setDirty(string $field, bool $isDirty = true): $this
设置单个字段的脏状态。
参数
-
string
$field 要设置或检查其状态的字段。
-
bool
$isDirty 可选 true 表示该字段已更改,false 表示该字段未更改。默认为 true。
返回
$this
setError() ¶ public
setError(string $field, array|string $errors, bool $overwrite = false): $this
设置单个字段的错误
示例
// Sets the error messages for a single field
$entity->setError('salary', ['must be numeric', 'must be a positive number']);
参数
-
string
$field 要获取错误的字段或要设置的错误数组。
-
array|string
$errors 要为
$field
设置的错误。-
bool
$overwrite 可选 是否覆盖
$field
的现有错误。
返回
$this
setErrors() ¶ public
setErrors(array $errors, bool $overwrite = false): $this
将错误消息设置为实体
示例
// Sets the error messages for multiple fields at once
$entity->setErrors(['salary' => ['message'], 'name' => ['another message']]);
参数
-
array
$errors 要设置的错误数组。
-
bool
$overwrite 可选 是否覆盖
$fields
的现有错误。
返回
$this
setHidden() ¶ public
setHidden(list<string> $fields, bool $merge = false): $this
设置隐藏字段。
参数
-
list<string>
$fields 要从数组导出中隐藏的字段数组。
-
bool
$merge 可选 将新字段与现有字段合并。默认情况下为 false。
返回
$this
setInvalid() ¶ public
setInvalid(array<string, mixed> $fields, bool $overwrite = false): $this
将字段设置为无效,不可修补到实体中。
这在批量操作时很有用,当需要在修补后获取错误消息的原始值时。此值无法修补到实体中,并且仅被复制到 _invalid 属性中以供调试或将其记录下来。
参数
-
array<string, mixed>
$fields 要设置的值。
-
bool
$overwrite 可选 是否覆盖
$field
的现有值。
返回
$this
setInvalidField() ¶ public
setInvalidField(string $field, mixed $value): $this
将字段设置为无效,不可修补到实体中。
参数
-
string
$field 要设置的值。
-
mixed
$value 要为
$field
设置的无效值。
返回
$this
setNew() ¶ public
setNew(bool $new): $this
设置此实体的状态。
使用 true
表示该实体尚未持久保存到数据库中,false
表示该实体已持久保存。
参数
-
bool
$new 指示此实体是否已持久保存。
返回
$this
setOriginalField() ¶ protected
setOriginalField(list<string>|string $field, bool $merge = true): $this
将给定的字段或字段列表设置为原始字段。 通常无需手动调用此方法。
参数
-
list<string>|string
$field 要设置为原始字段的字段名称或字段列表。
-
bool
$merge 可选
返回
$this
setVirtual() ¶ public
setVirtual(list<string> $fields, bool $merge = false): $this
设置此实体上的虚拟字段。
参数
-
list<string>
$fields 要作为虚拟字段处理的字段数组。
-
bool
$merge 可选 将新字段与现有字段合并。默认情况下为 false。
返回
$this
toArray() ¶ public
toArray(): array<string, mixed>
返回一个包含已设置为此实体的所有字段的数组
此方法将递归地将分配给字段的实体也转换为数组。
返回
array<string, mixed>
unset() ¶ public
unset(list<string>|string $field): $this
从此实体中删除字段或字段列表
示例
$entity->unset('name');
$entity->unset(['name', 'last_name']);
参数
-
list<string>|string
$field 要取消设置的字段。
返回
$this
属性详细信息
$_accessible ¶ protected
此实体中可以安全地进行批量赋值的字段映射,每个字段名称都指向一个布尔值,表示其状态。空数组表示没有字段可以进行批量赋值。
特殊的字段 '*' 也可以映射,这意味着映射中未定义的任何其他字段将取其值。例如,'*' => true
表示映射中未定义的任何字段默认情况下可用于批量赋值。
类型
array<string, bool>
$_accessors ¶ protected static
保存每个类的获取器/设置器的缓存列表
类型
array<string, array<string, array<string, string>>>
$_hidden ¶ protected
应该不包含在此实体的 JSON 或数组表示中的字段名称列表。
类型
list<string>
$_result ¶ protected
保存从搜索查询中传递到构造函数的 Result 对象实例。它可能包含有关此文档的搜索操作的额外信息,例如突出显示、分数和版本。
类型
Elastica\Result
$_virtual ¶ protected
应该包含在此实体的 JSON 或数组表示中的计算或虚拟字段列表。如果一个字段同时存在于 _hidden 和 _virtual 中,则该字段不会出现在实体的数组/JSON 版本中。
类型
list<string>