RepositoryInterface
in
Describes the methods that any class representing a data storage should comply with.
Table of Contents
Methods
- delete() : bool
- Delete a single entity.
- deleteAll() : int
- Deletes all records matching the provided conditions.
- exists() : bool
- Returns true if there is any record in this repository matching the specified conditions.
- find() : QueryInterface
- Creates a new Query for this repository and applies some defaults based on the type of search that was selected.
- get() : EntityInterface
- Returns a single record after finding it by its primary key, if no record is found this method throws an exception.
- getAlias() : string
- Returns the repository alias.
- getRegistryAlias() : string
- Returns the table registry key used to create this table instance.
- hasField() : bool
- Test to see if a Repository has a specific field/column.
- newEmptyEntity() : EntityInterface
- This creates a new entity object.
- newEntities() : array<string|int, EntityInterface>
- Create a list of entities + associated entities from an array.
- newEntity() : EntityInterface
- Create a new entity + associated entities from an array.
- patchEntities() : array<string|int, EntityInterface>
- Merges each of the elements passed in `$data` into the entities found in `$entities` respecting the accessible fields configured on the entities.
- patchEntity() : EntityInterface
- Merges the passed `$data` into `$entity` respecting the accessible fields configured on the entity. Returns the same entity after being altered.
- query() : QueryInterface
- Creates a new Query instance for this repository
- save() : EntityInterface|false
- Persists an entity based on the fields that are marked as dirty and returns the same entity after a successful save or false in case of any error.
- setAlias() : $this
- Sets the repository alias.
- setRegistryAlias() : $this
- Sets the table registry key used to create this table instance.
- updateAll() : int
- Update all matching records.
Methods
delete()
Delete a single entity.
public
delete(EntityInterface $entity[, ArrayAccess|array<string|int, mixed> $options = [] ]) : bool
Deletes an entity and possibly related associations from the database based on the 'dependent' option used when defining the association.
Parameters
- $entity : EntityInterface
-
The entity to remove.
- $options : ArrayAccess|array<string|int, mixed> = []
-
The options for the delete.
Return values
bool —success
deleteAll()
Deletes all records matching the provided conditions.
public
deleteAll(mixed $conditions) : int
This method will not trigger beforeDelete/afterDelete events. If you need those first load a collection of records and delete them.
This method will not execute on associations' cascade
attribute. You should
use database foreign keys + ON CASCADE rules if you need cascading deletes combined
with this method.
Parameters
- $conditions : mixed
-
Conditions to be used, accepts anything Query::where() can take.
Tags
Return values
int —Returns the number of affected rows.
exists()
Returns true if there is any record in this repository matching the specified conditions.
public
exists(array<string|int, mixed> $conditions) : bool
Parameters
- $conditions : array<string|int, mixed>
-
list of conditions to pass to the query
Return values
boolfind()
Creates a new Query for this repository and applies some defaults based on the type of search that was selected.
public
find([string $type = 'all' ][, array<string, mixed> $options = [] ]) : QueryInterface
Parameters
- $type : string = 'all'
-
the type of query to perform
- $options : array<string, mixed> = []
-
An array that will be passed to Query::applyOptions()
Return values
QueryInterfaceget()
Returns a single record after finding it by its primary key, if no record is found this method throws an exception.
public
get(mixed $primaryKey[, array<string, mixed> $options = [] ]) : EntityInterface
Example:
$id = 10;
$article = $articles->get($id);
$article = $articles->get($id, ['contain' => ['Comments]]);
Parameters
- $primaryKey : mixed
-
primary key value to find
- $options : array<string, mixed> = []
-
options accepted by
Table::find()
Tags
Return values
EntityInterfacegetAlias()
Returns the repository alias.
public
getAlias() : string
Return values
stringgetRegistryAlias()
Returns the table registry key used to create this table instance.
public
getRegistryAlias() : string
Return values
stringhasField()
Test to see if a Repository has a specific field/column.
public
hasField(string $field) : bool
Parameters
- $field : string
-
The field to check for.
Return values
bool —True if the field exists, false if it does not.
newEmptyEntity()
This creates a new entity object.
public
newEmptyEntity() : EntityInterface
Careful: This does not trigger any field validation. This entity can be persisted without validation error as empty record. Always patch in required fields before saving.
Return values
EntityInterfacenewEntities()
Create a list of entities + associated entities from an array.
public
newEntities(array<string|int, mixed> $data[, array<string, mixed> $options = [] ]) : array<string|int, EntityInterface>
This is most useful when hydrating request data back into entities. For example, in your controller code:
$articles = $this->Articles->newEntities($this->request->getData());
The hydrated entities can then be iterated and saved.
Parameters
- $data : array<string|int, mixed>
-
The data to build an entity with.
- $options : array<string, mixed> = []
-
A list of options for the objects hydration.
Return values
array<string|int, EntityInterface> —An array of hydrated records.
newEntity()
Create a new entity + associated entities from an array.
public
newEntity(array<string|int, mixed> $data[, array<string, mixed> $options = [] ]) : EntityInterface
This is most useful when hydrating request data back into entities. For example, in your controller code:
$article = $this->Articles->newEntity($this->request->getData());
The hydrated entity will correctly do an insert/update based on the primary key data existing in the database when the entity is saved. Until the entity is saved, it will be a detached record.
Parameters
- $data : array<string|int, mixed>
-
The data to build an entity with.
- $options : array<string, mixed> = []
-
A list of options for the object hydration.
Return values
EntityInterfacepatchEntities()
Merges each of the elements passed in `$data` into the entities found in `$entities` respecting the accessible fields configured on the entities.
public
patchEntities(iterable<string|int, EntityInterface> $entities, array<string|int, mixed> $data[, array<string, mixed> $options = [] ]) : array<string|int, EntityInterface>
Merging is done by matching the primary key in each of the elements in $data
and $entities
.
This is most useful when editing a list of existing entities using request data:
$article = $this->Articles->patchEntities($articles, $this->request->getData());
Parameters
- $entities : iterable<string|int, EntityInterface>
-
the entities that will get the data merged in
- $data : array<string|int, mixed>
-
list of arrays to be merged into the entities
- $options : array<string, mixed> = []
-
A list of options for the objects hydration.
Return values
array<string|int, EntityInterface>patchEntity()
Merges the passed `$data` into `$entity` respecting the accessible fields configured on the entity. Returns the same entity after being altered.
public
patchEntity(EntityInterface $entity, array<string|int, mixed> $data[, array<string, mixed> $options = [] ]) : EntityInterface
This is most useful when editing an existing entity using request data:
$article = $this->Articles->patchEntity($article, $this->request->getData());
Parameters
- $entity : EntityInterface
-
the entity that will get the data merged in
- $data : array<string|int, mixed>
-
key value list of fields to be merged into the entity
- $options : array<string, mixed> = []
-
A list of options for the object hydration.
Return values
EntityInterfacequery()
Creates a new Query instance for this repository
public
query() : QueryInterface
Return values
QueryInterfacesave()
Persists an entity based on the fields that are marked as dirty and returns the same entity after a successful save or false in case of any error.
public
save(EntityInterface $entity[, ArrayAccess|array<string|int, mixed> $options = [] ]) : EntityInterface|false
Parameters
- $entity : EntityInterface
-
the entity to be saved
- $options : ArrayAccess|array<string|int, mixed> = []
-
The options to use when saving.
Return values
EntityInterface|falsesetAlias()
Sets the repository alias.
public
setAlias(string $alias) : $this
Parameters
- $alias : string
-
Table alias
Return values
$thissetRegistryAlias()
Sets the table registry key used to create this table instance.
public
setRegistryAlias(string $registryAlias) : $this
Parameters
- $registryAlias : string
-
The key used to access this object.
Return values
$thisupdateAll()
Update all matching records.
public
updateAll(QueryExpression|Closure|array<string|int, mixed>|string $fields, mixed $conditions) : int
Sets the $fields to the provided values based on $conditions. This method will not trigger beforeSave/afterSave events. If you need those first load a collection of records and update them.
Parameters
- $fields : QueryExpression|Closure|array<string|int, mixed>|string
-
A hash of field => new value.
- $conditions : mixed
-
Conditions to be used, accepts anything Query::where() can take.
Return values
int —Count Returns the affected rows.