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
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Query
      • Rule
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

类 CounterCacheBehavior

CounterCache 行为

使模型能够缓存给定关系中的连接数量。

Post 模型属于 User 模型的示例

常规计数器缓存

[
    'Users' => [
        'post_count'
    ]
]

带作用域的计数器缓存

[
    'Users' => [
        'posts_published' => [
            'conditions' => [
                'published' => true
            ]
        ]
    ]
]

使用自定义查找的计数器缓存

[
    'Users' => [
        'posts_published' => [
            'finder' => 'published' // Will be using findPublished()
        ]
    ]
]

使用返回计数的 lambda 函数的计数器缓存 这等效于示例 #2

[
    'Users' => [
        'posts_published' => function (EventInterface $event, EntityInterface $entity, Table $table) {
            $query = $table->find('all')->where([
                'published' => true,
                'user_id' => $entity->get('user_id')
            ]);
            return $query->count();
         }
    ]
]

当使用 lambda 函数时,您可以返回 false 来禁用更新当前操作的计数器值。

如果字段脏了,则忽略更新字段

[
    'Users' => [
        'posts_published' => [
            'ignoreDirty' => true
        ]
    ]
]

您可以通过向保存操作发送 ignoreCounterCache 选项来完全禁用计数器更新

$this->Articles->save($article, ['ignoreCounterCache' => true]);
命名空间: Cake\ORM\Behavior

属性摘要

  • $_config protected
    array<string, mixed>

    运行时配置

  • $_configInitialized protected
    bool

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

  • $_defaultConfig protected
    array<string, mixed>

    默认配置

  • $_ignoreDirty protected
    array<string, array<string, bool>>

    存储应忽略的字段

  • $_reflectionCache protected static
    array<string, array>

    行为的反射方法缓存。

  • $_table protected
    Cake\ORM\Table

    表实例。

方法摘要

  • __construct() public

    构造函数

  • _configDelete() protected

    删除单个配置键。

  • _configRead() protected

    读取配置键。

  • _configWrite() protected

    写入配置键。

  • _getCount() protected

    获取并返回关联中单个字段的计数

  • _processAssociation() protected

    更新单个关联的计数器缓存

  • _processAssociations() protected

    迭代所有关联并更新计数器缓存。

  • _reflectionCache() protected

    获取此行为实现的方法

  • _resolveMethodAliases() protected

    删除别名方法,否则这些方法将因用户配置而重复。

  • _shouldUpdateCount() protected

    检查在给定一组条件的情况下是否应更新计数。

  • afterDelete() public

    afterDelete 回调。

  • afterSave() public

    afterSave 回调。

  • beforeSave() public

    beforeSave 回调。

  • 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 = [])

构造函数

将配置与默认配置合并,并存储在 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
如果尝试覆盖现有配置

_getCount() ¶ protected

_getCount(array<string, mixed> $config, array $conditions): int

获取并返回关联中单个字段的计数

参数
array<string, mixed> $config

单个字段的计数器缓存配置

array $conditions

传递给查询的附加条件

返回值
int

_processAssociation() ¶ protected

_processAssociation(Cake\Event\EventInterfaceCake\ORM\Table> $event, Cake\Datasource\EntityInterface $entity, Cake\ORM\Association $assoc, array $settings): void

更新单个关联的计数器缓存

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

事件实例。

Cake\Datasource\EntityInterface $entity

实体

Cake\ORM\Association $assoc

关联对象

array $settings

此关联的计数器缓存设置

返回值
void
抛出
RuntimeException
如果传递了无效的可调用对象。

_processAssociations() ¶ protected

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

迭代所有关联并更新计数器缓存。

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

事件实例。

Cake\Datasource\EntityInterface $entity

实体。

返回值
void

_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

_shouldUpdateCount() ¶ protected

_shouldUpdateCount(array $conditions): bool

检查在给定一组条件的情况下是否应更新计数。

参数
array $conditions

更新计数的条件。

返回值
bool

afterDelete() ¶ public

afterDelete(Cake\Event\EventInterfaceCake\ORM\Table> $event, Cake\Datasource\EntityInterface $entity, ArrayObject<string, mixed> $options): void

afterDelete 回调。

确保在删除记录时更新计数器缓存。

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

触发的 afterDelete 事件。

Cake\Datasource\EntityInterface $entity

被删除的实体。

ArrayObject<string, mixed> $options

查询的选项

返回值
void

afterSave() ¶ public

afterSave(Cake\Event\EventInterfaceCake\ORM\Table> $event, Cake\Datasource\EntityInterface $entity, ArrayObject<string, mixed> $options): void

afterSave 回调。

确保在创建或更新新记录时更新计数器缓存。

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

触发的 afterSave 事件。

Cake\Datasource\EntityInterface $entity

保存的实体。

ArrayObject<string, mixed> $options

查询的选项

返回值
void

beforeSave() ¶ public

beforeSave(Cake\Event\EventInterfaceCake\ORM\Table> $event, Cake\Datasource\EntityInterface $entity, ArrayObject<string, mixed> $options): void

beforeSave 回调。

检查是否有一个应该被忽略的字段被修改了。

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

触发的 beforeSave 事件。

Cake\Datasource\EntityInterface $entity

将要被保存的实体。

ArrayObject<string, mixed> $options

查询的选项

返回值
void

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

建议(但不要求)在子类的 config 属性中定义 implementedFinders,这样就不需要使用反射来推导出可用方法列表。有关示例,请参阅核心行为。

返回值
array
抛出
ReflectionException

implementedMethods() ¶ public

implementedMethods(): array

implementedMethods

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

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

在上面的例子中,调用 $table->method() 将调用 $behavior->method(),调用 $table->aliasedMethod() 将调用 $behavior->somethingElse()

建议(但不要求)在子类的 config 属性中定义 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>

$_ignoreDirty ¶ protected

存储应忽略的字段

类型
array<string, array<string, bool>>

$_reflectionCache ¶ protected static

行为的反射方法缓存。

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

类型
array<string, array>

$_table ¶ protected

表实例。

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

使用 CakePHP API 文档 生成