AbstractRepository
in package
implements
RepositoryInterface
Abstract base class for repositories.
Provides common CRUD functionality using the QueryBuilder with prepared statements for security.
Tags
Table of Contents
Interfaces
- RepositoryInterface
- Base interface for all repositories.
Properties
- $columnMap : array<string, string>
- Column mapping: entity property => database column.
- $primaryKey : string
- The primary key column name.
- $tableName : string
- The table name (without prefix).
Methods
- beginTransaction() : bool
- Begin a database transaction.
- commit() : bool
- Commit the current transaction.
- count() : int
- Count entities matching criteria.
- delete() : bool
- Delete an entity.
- exists() : bool
- Check if an entity with the given ID exists.
- find() : object|null
- Find an entity by its primary key.
- findAll() : array<int, object>
- Find all entities.
- findBy() : array<int, object>
- Find entities by criteria.
- findOneBy() : object|null
- Find a single entity by criteria.
- rollback() : bool
- Rollback the current transaction.
- save() : int
- Save an entity (insert or update).
- getEntityId() : int
- Get the ID from an entity.
- mapToEntity() : object
- Map a database row to an entity object.
- mapToRow() : array<string, scalar|null>
- Map an entity to a database row.
- query() : QueryBuilder
- Get a query builder for this repository's table.
- setEntityId() : void
- Set the ID on an entity.
Properties
$columnMap
Column mapping: entity property => database column.
protected
array<string, string>
$columnMap
= []
$primaryKey
The primary key column name.
protected
string
$primaryKey
= 'id'
$tableName
The table name (without prefix).
protected
string
$tableName
Methods
beginTransaction()
Begin a database transaction.
public
beginTransaction() : bool
Return values
boolcommit()
Commit the current transaction.
public
commit() : bool
Return values
boolcount()
Count entities matching criteria.
public
count([array<string, mixed> $criteria = [] ]) : int
Parameters
- $criteria : array<string, mixed> = []
Return values
int —The count
delete()
Delete an entity.
public
delete(object|int $entityOrId) : bool
Parameters
- $entityOrId : object|int
-
The entity or its ID
Tags
Return values
bool —True if deleted, false if not found
exists()
Check if an entity with the given ID exists.
public
exists(int $id) : bool
Parameters
- $id : int
-
The entity ID
Return values
boolfind()
Find an entity by its primary key.
public
find(int $id) : object|null
Parameters
- $id : int
-
The entity ID
Tags
Return values
object|null —The entity or null if not found
findAll()
Find all entities.
public
findAll() : array<int, object>
Return values
array<int, object> —Array of entities
findBy()
Find entities by criteria.
public
findBy(array<string, mixed> $criteria[, array<string, string>|null $orderBy = null ][, int|null $limit = null ][, int|null $offset = null ]) : array<int, object>
Parameters
- $criteria : array<string, mixed>
- $orderBy : array<string, string>|null = null
- $limit : int|null = null
-
Maximum results
- $offset : int|null = null
-
Offset for pagination
Return values
array<int, object> —Array of matching entities
findOneBy()
Find a single entity by criteria.
public
findOneBy(array<string, mixed> $criteria) : object|null
Parameters
- $criteria : array<string, mixed>
Tags
Return values
object|nullrollback()
Rollback the current transaction.
public
rollback() : bool
Return values
boolsave()
Save an entity (insert or update).
public
save(object $entity) : int
Parameters
- $entity : object
-
The entity to save
Tags
Return values
int —The entity ID (useful for inserts)
getEntityId()
Get the ID from an entity.
protected
abstract getEntityId(object $entity) : int
Parameters
- $entity : object
-
The entity
Tags
Return values
int —The entity ID
mapToEntity()
Map a database row to an entity object.
protected
abstract mapToEntity(array<string, mixed> $row) : object
Parameters
- $row : array<string, mixed>
-
Database row
Tags
Return values
object —The entity
mapToRow()
Map an entity to a database row.
protected
abstract mapToRow(object $entity) : array<string, scalar|null>
Parameters
- $entity : object
-
The entity
Tags
Return values
array<string, scalar|null> —Database row data
query()
Get a query builder for this repository's table.
protected
query() : QueryBuilder
Return values
QueryBuildersetEntityId()
Set the ID on an entity.
protected
abstract setEntityId(object $entity, int $id) : void
Parameters
- $entity : object
-
The entity
- $id : int
-
The ID to set