CakePHP
  • 文档
    • 书籍
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识和商标
  • 商业解决方案
  • 周边商品
  • 公路旅行
  • 团队
  • 社区
    • 社区
    • 参与
    • 问题(Github)
    • Bakery
    • 精选资源
    • 培训
    • 聚会
    • 我的 CakePHP
    • CakeFest
    • 新闻稿
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • 帮助和支持
    • 论坛
    • Stack Overflow
    • IRC
    • Slack
    • 付费支持
CakePHP

C CakePHP 5.1 Chiffon API

  • 项目
    • CakePHP
      • CakePHP
      • Chronos
      • Elastic Search
      • 队列
  • 版本
    • 5.1
      • 5.1
      • 5.0
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

命名空间

  • 全局
  • Cake
    • 缓存
    • 集合
    • 命令
    • 控制台
    • 控制器
    • 核心
    • 数据库
    • 数据源
    • 错误
    • 事件
    • 表单
    • HTTP
    • 国际化
    • 日志
    • 邮件
    • 网络
    • ORM
      • 关联
      • 行为
        • 翻译
      • 异常
      • 定位器
      • 查询
      • 规则
    • 路由
    • 测试套件
    • 实用工具
    • 验证
    • 视图

类 TimestampBehavior

类 TimestampBehavior

命名空间: Cake\ORM\Behavior

属性摘要

  • $_config protected
    array<string, mixed>

    运行时配置

  • $_configInitialized protected
    bool

    配置属性是否已使用默认值进行配置

  • $_defaultConfig protected
    array<string, mixed>

    默认配置

  • $_reflectionCache protected static
    array<string, array>

    行为的反射方法缓存。

  • $_table protected
    Cake\ORM\Table

    表实例。

  • $_ts protected
    Cake\I18n\DateTime|null

    当前时间戳

方法摘要

  • __construct() public

    构造函数

  • _configDelete() protected

    删除单个配置键。

  • _configRead() protected

    读取配置键。

  • _configWrite() protected

    写入配置键。

  • _reflectionCache() protected

    获取此行为实现的方法

  • _resolveMethodAliases() protected

    删除别名方法,否则这些方法将被用户代码配置复制。

  • _updateField() protected

    更新字段,如果它尚未更新

  • configShallow() public

    将提供的配置与现有配置合并。与 config() 不同,config() 对嵌套键执行递归合并,此方法执行简单合并。

  • getConfig() public

    返回配置。

  • getConfigOrFail() public

    返回此特定键的配置。

  • handleEvent() public

    只有一个事件处理程序,它可以被配置为针对任何事件调用

  • implementedEvents() public

    implementedEvents

  • implementedFinders() public

    implementedFinders

  • implementedMethods() public

    implementedMethods

  • initialize() public

    初始化钩子

  • setConfig() public

    设置配置。

  • table() public

    获取此行为绑定的表实例。

  • timestamp() public

    获取或设置要使用的时间戳

  • touch() public

    触碰实体

  • verifyConfig() public

    verifyConfig

方法详情

__construct() ¶ public

__construct(Cake\ORM\Table $table, array<string, mixed> $config = [])

构造函数

将配置与默认配置合并并存储在配置属性中

参数
Cake\ORM\Table $table

此行为附加到的表。

array<string, mixed> $config optional

此行为的配置。

_configDelete() ¶ protected

_configDelete(string $key): void

删除单个配置键。

参数
string $key

要删除的键。

返回值
void
抛出
Cake\Core\Exception\CakeException
如果尝试覆盖现有配置

_configRead() ¶ protected

_configRead(string|null $key): mixed

读取配置键。

参数
string|null $key

要读取的键。

返回值
mixed

_configWrite() ¶ protected

_configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void

写入配置键。

参数
array<string, mixed>|string $key

要写入的键。

mixed $value

要写入的值。

string|bool $merge optional

true 为递归合并,'shallow' 为简单合并,false 为覆盖,默认为 false。

返回值
void
抛出
Cake\Core\Exception\CakeException
如果尝试覆盖现有配置

_reflectionCache() ¶ protected

_reflectionCache(): array

获取此行为实现的方法

使用 implementedEvents() 方法排除回调方法。以 _ 开头的 方法将被忽略,在 Cake\ORM\Behavior 上声明的方法也将被忽略

返回值
array
抛出
ReflectionException

_resolveMethodAliases() ¶ protected

_resolveMethodAliases(string $key, array<string, mixed> $defaults, array<string, mixed> $config): array

删除别名方法,否则这些方法将被用户代码配置复制。

参数
string $key

要过滤的键。

array<string, mixed> $defaults

默认方法映射。

array<string, mixed> $config

自定义方法映射。

返回值
array

_updateField() ¶ protected

_updateField(Cake\Datasource\EntityInterface $entity, string $field, bool $refreshTimestamp): void

更新字段,如果它尚未更新

参数
Cake\Datasource\EntityInterface $entity

实体实例。

string $field

字段名称

bool $refreshTimestamp

是否刷新时间戳。

返回值
void

configShallow() ¶ public

configShallow(array<string, mixed>|string $key, mixed|null $value = null): $this

将提供的配置与现有配置合并。与 config() 不同,config() 对嵌套键执行递归合并,此方法执行简单合并。

设置特定值

$this->configShallow('key', $value);

设置嵌套值

$this->configShallow('some.nested.key', $value);

同时更新多个配置设置

$this->configShallow(['one' => 'value', 'another' => 'value']);
参数
array<string, mixed>|string $key

要设置的键,或完整的配置数组。

mixed|null $value optional

要设置的值。

返回值
$this

getConfig() ¶ public

getConfig(string|null $key = null, mixed $default = null): mixed

返回配置。

用法

读取整个配置

$this->getConfig();

读取特定值

$this->getConfig('key');

读取嵌套值

$this->getConfig('some.nested.key');

使用默认值读取

$this->getConfig('some-key', 'default-value');
参数
string|null $key optional

要获取的键,或整个配置的 null。

mixed $default optional

键不存在时的返回值。

返回值
mixed

getConfigOrFail() ¶ public

getConfigOrFail(string $key): mixed

返回此特定键的配置。

此键的配置值必须存在,不能为 null。

参数
string $key

要获取的键。

返回值
mixed
抛出
InvalidArgumentException

handleEvent() ¶ public

handleEvent(Cake\Event\EventInterfaceCake\ORM\Table> $event, Cake\Datasource\EntityInterface $entity): void

只有一个事件处理程序,它可以被配置为针对任何事件调用

参数
Cake\Event\EventInterfaceCake\ORM\Table> $event

事件实例。

Cake\Datasource\EntityInterface $entity

实体实例。

返回值
void
抛出
UnexpectedValueException
如果字段的 when 值定义错误
UnexpectedValueException
当事件的值不是 'always'、'new' 或 'existing' 时

implementedEvents() ¶ public

implementedEvents(): array<string, mixed>

implementedEvents

此行为的实现事件取决于配置

返回值
array<string, mixed>

implementedFinders() ¶ public

implementedFinders(): array

implementedFinders

提供行为实现的查找器别名->方法名映射。示例

 [
   'this' => 'findThis',
   'alias' => 'findMethodName'
 ]

在上述示例中,对 $table->find('this') 的调用将调用 $behavior->findThis(),而对 $table->find('alias') 的调用将调用 $behavior->findMethodName()

建议(但不是必需)在子类的配置属性中定义 implementedFinders,这样就不需要使用反射来推导出可用方法列表。查看核心行为以获取示例

返回值
array
抛出
ReflectionException

implementedMethods() ¶ public

implementedMethods(): array

implementedMethods

提供行为实现的方法别名->方法名映射。示例

 [
   'method' => 'method',
   'aliasedMethod' => 'somethingElse'
 ]

在上述示例中,对 $table->method() 的调用将调用 $behavior->method(),而对 $table->aliasedMethod() 的调用将调用 $behavior->somethingElse()

建议(但不是必需)在子类的配置属性中定义 implementedFinders,这样就不需要使用反射来推导出可用方法列表。查看核心行为以获取示例

返回值
array
抛出
ReflectionException

initialize() ¶ public

initialize(array<string, mixed> $config): void

初始化钩子

如果指定了事件,请不要将它们与现有事件合并,而是覆盖要监听的事件。

参数
array<string, mixed> $config

此行为的配置。

返回值
void

setConfig() ¶ public

setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this

设置配置。

用法

设置特定值

$this->setConfig('key', $value);

设置嵌套值

$this->setConfig('some.nested.key', $value);

同时更新多个配置设置

$this->setConfig(['one' => 'value', 'another' => 'value']);
参数
array<string, mixed>|string $key

要设置的键,或完整的配置数组。

mixed|null $value optional

要设置的值。

bool $merge 可选

是否递归合并或覆盖现有配置,默认为 true。

返回值
$this
抛出
Cake\Core\Exception\CakeException
当尝试设置无效的键时。

table() ¶ public

table(): Cake\ORM\Table

获取此行为绑定的表实例。

返回值
Cake\ORM\Table

timestamp() ¶ public

timestamp(DateTimeInterface|null $ts = null, bool $refreshTimestamp = false): Cake\I18n\DateTime

获取或设置要使用的时间戳

将时间戳设置为给定的 DateTime 对象,或者如果没有传递,则设置为一个新的 DateTime 对象。如果传递了显式日期时间,则配置选项 refreshTimestamp 会自动设置为 false。

参数
DateTimeInterface|null $ts 可选

时间戳

bool $refreshTimestamp 可选

如果为 true,则会刷新时间戳。

返回值
Cake\I18n\DateTime

touch() ¶ public

touch(Cake\Datasource\EntityInterface $entity, string $eventName = 'Model.beforeSave'): bool

触碰实体

更新实体的时间戳字段。对于配置为始终或仅在现有记录中更新的任何字段,请更新时间戳值。此方法将覆盖任何预先存在的价值。

参数
Cake\Datasource\EntityInterface $entity

实体实例。

string $eventName 可选

事件名称。

返回值
bool

verifyConfig() ¶ public

verifyConfig(): void

verifyConfig

检查已实现的键是否包含指向可调用函数的值。

返回值
void
抛出
Cake\Core\Exception\CakeException
如果配置无效。

属性详情

$_config ¶ protected

运行时配置

类型
array<string, mixed>

$_configInitialized ¶ protected

配置属性是否已使用默认值进行配置

类型
bool

$_defaultConfig ¶ protected

默认配置

这些在使用行为时与用户提供的配置合并。

events - 一个以事件名称为键的数组,其中包含要更新的字段,以及在给定事件时更新时间,对于何时更新字段,可能的取值是 "always"、"new" 或 "existing",分别表示始终设置字段值,仅在新建记录时设置,或仅在已有记录时设置。

refreshTimestamp - 如果为 true(默认值),则使用的时间戳将是代码执行时的当前时间,要设置为显式日期时间值,请将 refreshTimestamp 设置为 false,并在使用前调用行为类上的 setTimestamp() 方法。

类型
array<string, mixed>

$_reflectionCache ¶ protected static

行为的反射方法缓存。

存储每个类的反射方法和查找器方法。这可以避免在单个进程中多次反射同一个类。

类型
array<string, array>

$_table ¶ protected

表实例。

类型
Cake\ORM\Table

$_ts ¶ protected

当前时间戳

类型
Cake\I18n\DateTime|null
OpenHub
Pingping
Linode
  • 业务解决方案
  • 展示
  • 文档
  • 书籍
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识和商标
  • 社区
  • 参与
  • 问题(Github)
  • Bakery
  • 精选资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 新闻稿
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • 帮助和支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成