CakePHP
  • 文档
    • 书籍
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识和商标
  • 商业解决方案
  • 纪念品
  • 公路旅行
  • 团队
  • 社区
    • 社区
    • 参与
    • 问题 (Github)
    • 烘焙坊
    • 特色资源
    • 培训
    • 聚会
    • 我的 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
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
      • Association
      • Behavior
      • Exception
      • Locator
      • Query
      • Rule
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

类行为

行为的基类。

行为允许你模拟 Mixin,并创建可重用的应用程序逻辑块,这些逻辑块可以在多个模型中重复使用。行为还提供了一种方法来钩入模型回调并增强其行为。

Mixin 方法

行为可以通过声明公共方法来提供类似 Mixin 的功能。这些方法可以在添加了该行为的表上访问。

function doSomething($arg1, $arg2) {
  // do something
}

使用方法类似于 $table->doSomething($arg1, $arg2);。

回调方法

行为可以监听表上触发的任何事件。默认情况下,CakePHP 提供了许多生命周期事件,你的行为可以监听这些事件。

  • beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options, boolean $primary) 在每次查找操作之前触发。通过停止事件并提供返回值,你可以完全绕过查找操作。对 $query 实例所做的任何更改都将保留在查找的剩余部分中。$primary 参数指示这是根查询,还是关联查询。

  • buildValidator(EventInterface $event, Validator $validator, string $name) 当 $name 标识的验证器对象正在构建时触发。你可以使用此回调添加验证规则或添加验证提供者。

  • buildRules(EventInterface $event, RulesChecker $rules) 当表规则检查对象的构建时触发。你可以使用此回调向集合中添加更多规则。

  • beforeRules(EventInterface $event, EntityInterface $entity, ArrayObject $options, $operation) 在使用规则检查器验证实体之前触发。通过停止此事件,你可以返回规则检查操作的最终值。

  • afterRules(EventInterface $event, EntityInterface $entity, ArrayObject $options, bool $result, $operation) 在实体上检查规则之后触发。通过停止此事件,你可以返回规则检查操作的最终值。

  • beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options) 在保存每个实体之前触发。停止此事件将中止保存操作。当事件被停止时,将返回事件的结果。

  • afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options) 在实体保存后触发。

  • beforeDelete(EventInterface $event, EntityInterface $entity, ArrayObject $options) 在删除实体之前触发。通过停止此事件,你将中止删除操作。

  • afterDelete(EventInterface $event, EntityInterface $entity, ArrayObject $options) 在实体删除后触发。

除了核心事件之外,行为还可以响应从你的 Table 类触发的任何事件,包括自定义的应用程序特定事件。

你可以在附加行为时使用 priority 设置来设置行为回调的优先级。这将为行为提供的所有回调设置优先级。

查找器方法

行为可以提供查找器方法,这些方法会钩入表的 find() 方法。自定义查找器是提供与你的行为相关的预设查询的绝佳方法。例如,SluggableBehavior 可以提供一个 find('slugged') 查找器。行为查找器的实现方式与其他查找器相同。以 find 开头的任何方法都将被设置为查找器。你的查找器方法应预期以下参数

findSlugged(SelectQuery $query, array $options)
命名空间: Cake\ORM
参见: \Cake\ORM\Table::addBehavior()
参见: \Cake\Event\EventManager

属性摘要

  • $_config protected
    array<string, mixed>

    运行时配置

  • $_configInitialized protected
    bool

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

  • $_defaultConfig protected
    array<string, mixed>

    默认配置

  • $_reflectionCache protected static
    array<string, array>

    行为的反射方法缓存。

  • $_table protected
    Cake\ORM\Table

    表实例。

方法摘要

  • __construct() public

    构造函数

  • _configDelete() protected

    删除单个配置键。

  • _configRead() protected

    读取配置键。

  • _configWrite() protected

    写入配置键。

  • _reflectionCache() protected

    获取此行为实现的方法

  • _resolveMethodAliases() protected

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

  • configShallow() public

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

  • getConfig() public

    返回配置。

  • getConfigOrFail() public

    返回此特定键的配置。

  • implementedEvents() public

    获取此行为感兴趣的模型回调。

  • implementedFinders() public

    implementedFinders

  • implementedMethods() public

    implementedMethods

  • initialize() public

    构造函数钩子方法。

  • setConfig() public

    设置配置。

  • table() 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

configShallow() ¶ public

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

将提供的配置与现有配置合并。与 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

implementedEvents() ¶ public

implementedEvents(): array<string, mixed>

获取此行为感兴趣的模型回调。

通过定义回调方法之一,假设行为对相关事件感兴趣。

如果需要添加非传统事件侦听器,或如果希望行为侦听非标准事件,请覆盖此方法。

返回
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 optional

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

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

table() ¶ public

table(): Cake\ORM\Table

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

返回
Cake\ORM\Table

verifyConfig() ¶ public

verifyConfig(): void

verifyConfig

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

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

属性详情

$_config ¶ protected

运行时配置

类型
array<string, mixed>

$_configInitialized ¶ protected

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

类型
bool

$_defaultConfig ¶ protected

默认配置

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

类型
array<string, mixed>

$_reflectionCache ¶ protected static

行为的反射方法缓存。

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

类型
array<string, array>

$_table ¶ protected

表实例。

类型
Cake\ORM\Table
OpenHub
Pingping
Linode
  • 商业解决方案
  • 展示
  • 文档
  • 书籍
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识和商标
  • 社区
  • 参与
  • 问题 (Github)
  • 烘焙坊
  • 特色资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 时事通讯
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • 帮助与支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成