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
      • Queue
  • 版本
    • 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
      • Exception
      • Locator
      • Paging
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

接口 RepositoryInterface

描述任何代表数据存储的类应遵守的方法。

命名空间: Cake\Datasource

方法概要

  • aliasField() public

    使用存储库的当前别名来为字段设置别名。

  • delete() public

    删除单个实体。

  • deleteAll() public

    删除与提供条件匹配的所有记录。

  • exists() public

    如果此存储库中存在任何与指定条件匹配的记录,则返回 true。

  • find() public

    为该存储库创建一个新的查询,并根据所选搜索类型应用一些默认值。

  • get() public

    通过其主键找到记录后返回单个记录,如果找不到记录,则此方法会抛出异常。

  • getAlias() public

    返回存储库别名。

  • getRegistryAlias() public

    返回用于创建此表实例的表注册表键。

  • hasField() public

    测试存储库是否具有特定字段/列。

  • newEmptyEntity() public

    这将创建一个新的实体对象。

  • newEntities() public

    从数组中创建实体列表和关联实体。

  • newEntity() public

    从数组中创建一个新的实体和关联实体。

  • patchEntities() public

    将 $data 中的每个元素合并到 $entities 中找到的实体中,尊重实体上配置的可访问字段。合并是通过匹配 $data 和 $entities 中每个元素的主键来完成的。

  • patchEntity() public

    将传递的 $data 合并到 $entity 中,尊重实体上配置的可访问字段。在被更改后返回相同的实体。

  • query() public

    为该存储库创建一个新的 Query 实例

  • save() public

    根据标记为脏的字段持久化实体,并在成功保存后返回相同的实体,或者在出现任何错误时返回 false。

  • setAlias() public

    设置存储库别名。

  • setRegistryAlias() public

    设置用于创建此表实例的表注册表键。

  • updateAll() public

    更新所有匹配的记录。

方法详情

aliasField() ¶ public

aliasField(string $field): string

使用存储库的当前别名来为字段设置别名。

如果字段已设置别名,则将执行无操作。

参数
string $field

要设置别名的字段。

返回
string

delete() ¶ public

delete(Cake\Datasource\EntityInterface $entity, array<string, mixed> $options = []): bool

删除单个实体。

根据在定义关联时使用的“dependent”选项从数据库中删除实体以及可能相关的关联。

参数
Cake\Datasource\EntityInterface $entity

要删除的实体。

array<string, mixed> $options optional

删除的选项。

返回
bool

deleteAll() ¶ public

deleteAll(Closure|array|string|null $conditions): int

删除与提供条件匹配的所有记录。

此方法不会触发 beforeDelete/afterDelete 事件。如果您需要这些,请先加载一个记录集合,然后删除它们。

此方法不会在关联的 cascade 属性上执行。如果您需要级联删除与该方法结合使用,则应使用数据库外键 + ON CASCADE 规则。

参数
Closure|array|string|null $conditions

要使用的条件,接受 Query::where() 可以接受的任何内容。

返回
int
另请参见
\Cake\Datasource\RepositoryInterface::delete()

exists() ¶ public

exists(Closure|array|string|null $conditions): bool

如果此存储库中存在任何与指定条件匹配的记录,则返回 true。

参数
Closure|array|string|null $conditions

要传递给查询的条件列表

返回
bool

find() ¶ public

find(string $type = 'all', mixed ...$args): Cake\Datasource\QueryInterface

为该存储库创建一个新的查询,并根据所选搜索类型应用一些默认值。

参数
string $type optional

要执行的查询类型

mixed ...$args

与查找器特定参数匹配的参数

返回
Cake\Datasource\QueryInterface

get() ¶ public

get(mixed $primaryKey, array|string $finder = 'all', Psr\SimpleCache\CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args): Cake\Datasource\EntityInterface

通过其主键找到记录后返回单个记录,如果找不到记录,则此方法会抛出异常。

示例

$id = 10;
$article = $articles->get($id);

$article = $articles->get($id, ['contain' => ['Comments]]);
参数
mixed $primaryKey

要查找的主键值

array|string $finder optional

要使用的查找器。传递选项数组已弃用。

Psr\SimpleCache\CacheInterface|string|null $cache optional

要使用的缓存配置。默认为 null,即不缓存。

Closure|string|null $cacheKey optional

要使用的缓存键。如果 $cache 不为 null,则在未提供时会自动生成一个。

mixed ...$args
返回
Cake\Datasource\EntityInterface
抛出
Cake\Datasource\Exception\RecordNotFoundException
如果找不到具有该 ID 的记录
另请参见
\Cake\Datasource\RepositoryInterface::find()

getAlias() ¶ public

getAlias(): string

返回存储库别名。

返回
string

getRegistryAlias() ¶ public

getRegistryAlias(): string

返回用于创建此表实例的表注册表键。

返回
string

hasField() ¶ public

hasField(string $field): bool

测试存储库是否具有特定字段/列。

参数
string $field

要检查的字段。

返回
bool

newEmptyEntity() ¶ public

newEmptyEntity(): Cake\Datasource\EntityInterface

这将创建一个新的实体对象。

注意:这不会触发任何字段验证。此实体可以作为空记录持久化而不会出现验证错误。在保存之前,请始终将必需字段修补到实体中。

返回
Cake\Datasource\EntityInterface

newEntities() ¶ public

newEntities(array $data, array<string, mixed> $options = []): arrayCake\Datasource\EntityInterface>

从数组中创建实体列表和关联实体。

这在将请求数据重新填充到实体中时非常有用。例如,在您的控制器代码中

$articles = $this->Articles->newEntities($this->request->getData());

然后可以迭代和保存已填充的实体。

参数
array $data

用于构建实体的数据。

array<string, mixed> $options optional

用于对象填充的选项列表。

返回
arrayCake\Datasource\EntityInterface>

newEntity() ¶ public

newEntity(array $data, array<string, mixed> $options = []): Cake\Datasource\EntityInterface

从数组中创建一个新的实体和关联实体。

这在将请求数据重新填充到实体中时非常有用。例如,在您的控制器代码中

$article = $this->Articles->newEntity($this->request->getData());

已填充的实体将在保存实体时根据数据库中存在的主键数据正确地执行插入/更新。在保存实体之前,它将是一个分离的记录。

参数
array $data

用于构建实体的数据。

array<string, mixed> $options optional

用于对象填充的选项列表。

返回
Cake\Datasource\EntityInterface

patchEntities() ¶ public

patchEntities(iterableCake\Datasource\EntityInterface> $entities, array $data, array<string, mixed> $options = []): arrayCake\Datasource\EntityInterface>

将 $data 中的每个元素合并到 $entities 中找到的实体中,尊重实体上配置的可访问字段。合并是通过匹配 $data 和 $entities 中每个元素的主键来完成的。

当使用请求数据编辑现有实体列表时,此方法非常有用。

$article = $this->Articles->patchEntities($articles, $this->request->getData());
参数
iterableCake\Datasource\EntityInterface> $entities

将要合并数据的实体。

array $data

要合并到实体中的数组列表。

array<string, mixed> $options optional

用于对象填充的选项列表。

返回
arrayCake\Datasource\EntityInterface>

patchEntity() ¶ public

patchEntity(Cake\Datasource\EntityInterface $entity, array $data, array<string, mixed> $options = []): Cake\Datasource\EntityInterface

将传递的 $data 合并到 $entity 中,尊重实体上配置的可访问字段。在被更改后返回相同的实体。

当使用请求数据编辑现有实体时,此方法非常有用。

$article = $this->Articles->patchEntity($article, $this->request->getData());
参数
Cake\Datasource\EntityInterface $entity

将要合并数据的实体。

array $data

要合并到实体中的键值对字段列表。

array<string, mixed> $options optional

用于对象填充的选项列表。

返回
Cake\Datasource\EntityInterface

query() ¶ public

query(): Cake\Datasource\QueryInterface

为该存储库创建一个新的 Query 实例

返回
Cake\Datasource\QueryInterface

save() ¶ public

save(Cake\Datasource\EntityInterface $entity, array<string, mixed> $options = []): Cake\Datasource\EntityInterface|false

根据标记为脏的字段持久化实体,并在成功保存后返回相同的实体,或者在出现任何错误时返回 false。

参数
Cake\Datasource\EntityInterface $entity

要保存的实体。

array<string, mixed> $options optional

保存时要使用的选项。

返回
Cake\Datasource\EntityInterface|false

setAlias() ¶ public

setAlias(string $alias): $this

设置存储库别名。

参数
string $alias

表别名。

返回
$this

setRegistryAlias() ¶ public

setRegistryAlias(string $registryAlias): $this

设置用于创建此表实例的表注册表键。

参数
string $registryAlias

用于访问此对象的键。

返回
$this

updateAll() ¶ public

updateAll(Closure|array|string $fields, Closure|array|string|null $conditions): int

更新所有匹配的记录。

根据 $conditions 将 $fields 设置为提供的值。此方法 *不会* 触发 beforeSave/afterSave 事件。如果您需要这些事件,请先加载记录集合并更新它们。

参数
Closure|array|string $fields

字段=>新值的哈希。

Closure|array|string|null $conditions

要使用的条件,接受 Query::where() 可以接受的任何内容。

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

使用 CakePHP API 文档 生成