Medical Appointments API

BaseRepository
in package

BaseRepository class for working with entities implementing EntityInterface.

This generic repository class provides basic CRUD operations for entities that implement the EntityInterface. It allows finding all entities, finding one entity by a specified condition, finding an entity by its ID, finding entities by a set of conditions, saving new entities, and updating existing entities. The repository assumes that the entity implements methods like 'table', 'primaryKey', 'fields', 'values', 'valuesAsMap', and 'bind'. These methods are used to interact with the entity's database table and data. Note: The actual implementation of the 'EntityInterface' and its related methods should be provided by the entities themselves.

Tags
template

T of BaseEntity

author

Luis Graciano contacto@luisgraciano.dev

Table of Contents

$entity  : mixed
$fields  : array<string|int, mixed>
$primaryKey  : array<string|int, mixed>
$table  : string
findAll()  : array<string|int, T>
Find all entities.
findById()  : T|null
Find an entity by its ID.
save()  : T|null
Save a new entity to the database.
update()  : bool
Update an existing entity in the database.
__construct()  : mixed
BaseRepository constructor.
buildEntity()  : T
Build an entity from the database row data.
fetchAll()  : array<string|int, T>
Fetch all entities based on the PDOStatement.
fetchOne()  : T|null
Fetch one entity based on the PDOStatement.
findBy()  : array<string|int, T>
Find entities based on a set of conditions.
findOne()  : T|null
Find one entity based on a specified condition.
getPDO()  : PDO
Get the PDO instance for database connection.
getPrimaryKeyCondition()  : string
Build the primary key condition for the query.
updateBy()  : bool
Update entities based on a set of conditions.
buildQuery()  : PDOStatement
Build a PDOStatement for querying the database based on the specified conditions.

Properties

$entity

protected mixed $entity

The class name of the entity that the repository works with.

$fields

protected array<string|int, mixed> $fields

The fields of the entity.

$primaryKey

protected array<string|int, mixed> $primaryKey

The primary key of the entity.

Methods

findAll()

Find all entities.

public findAll([PDO|null $pdo = null ]) : array<string|int, T>
Parameters
$pdo : PDO|null = null

The PDO instance for database connection (optional).

Tags
throws
UnhandledException

If an error occurs while preparing the PDOStatement.

Return values
array<string|int, T>

An array of entities.

findById()

Find an entity by its ID.

public findById(mixed $id[, PDO|null $pdo = null ]) : T|null
Parameters
$id : mixed

The ID of the entity.

$pdo : PDO|null = null

The PDO instance for database connection (optional).

Tags
throws
UnhandledException

If an error occurs while preparing the PDOStatement.

Return values
T|null

The entity found, or null if not found.

save()

Save a new entity to the database.

public save(T $entity[, PDO|null $pdo = null ]) : T|null
Parameters
$entity : T

The entity to be saved.

$pdo : PDO|null = null

The PDO instance for database connection.

Tags
throws
UnhandledException

If an error occurs while saving the entity.

Return values
T|null

The saved entity.

update()

Update an existing entity in the database.

public update(T $entity[, PDO|null $pdo = null ]) : bool
Parameters
$entity : T

The entity to be updated.

$pdo : PDO|null = null

The PDO instance for database connection (optional).

Tags
throws
UnhandledException

If an error occurs while preparing the PDOStatement.

Return values
bool

True if the update was successful, false otherwise.

__construct()

BaseRepository constructor.

protected __construct(class-string<\App\Repository\T> $entity) : mixed
Parameters
$entity : class-string<\App\Repository\T>

The class name of the entity that the repository works with.

Return values
mixed

buildEntity()

Build an entity from the database row data.

protected buildEntity(array<string|int, mixed> $data) : T
Parameters
$data : array<string|int, mixed>

The associative array representing the database row data.

Return values
T

The built entity.

fetchAll()

Fetch all entities based on the PDOStatement.

protected fetchAll(PDOStatement $PDOStatement) : array<string|int, T>
Parameters
$PDOStatement : PDOStatement

The prepared PDOStatement for the query.

Return values
array<string|int, T>

An array of entities.

fetchOne()

Fetch one entity based on the PDOStatement.

protected fetchOne(PDOStatement $PDOStatement) : T|null
Parameters
$PDOStatement : PDOStatement

The prepared PDOStatement for the query.

Return values
T|null

The entity found, or null if not found.

findBy()

Find entities based on a set of conditions.

protected findBy(array<string, mixed> $where[, PDO|null $pdo = null ]) : array<string|int, T>
Parameters
$where : array<string, mixed>

An associative array of conditions for the query.

$pdo : PDO|null = null

The PDO instance for database connection (optional).

Tags
throws
UnhandledException

If an error occurs while preparing the PDOStatement.

Return values
array<string|int, T>

An array of entities.

findOne()

Find one entity based on a specified condition.

protected findOne(array<string, mixed> $where[, PDO|null $pdo = null ]) : T|null
Parameters
$where : array<string, mixed>

An associative array of conditions for the query.

$pdo : PDO|null = null

The PDO instance for database connection (optional).

Tags
throws
UnhandledException

If an error occurs while preparing the PDOStatement.

Return values
T|null

The entity found, or null if not found.

getPDO()

Get the PDO instance for database connection.

protected getPDO([PDO|null $pdo = null ]) : PDO
Parameters
$pdo : PDO|null = null

The PDO instance (optional).

Tags
throws
UnhandledException

If an error occurs while connecting to the database.

Return values
PDO

The PDO instance for database connection.

getPrimaryKeyCondition()

Build the primary key condition for the query.

protected getPrimaryKeyCondition() : string
Return values
string

The primary key condition for the query.

updateBy()

Update entities based on a set of conditions.

protected updateBy(array<string|int, mixed> $data, array<string|int, mixed> $where[, PDO|null $pdo = null ]) : bool
Parameters
$data : array<string|int, mixed>

The data to be updated.

$where : array<string|int, mixed>

Conditions for the query.

$pdo : PDO|null = null

The PDO instance for database connection (optional).

Tags
throws
UnhandledException

If an error occurs while preparing the PDOStatement.

Return values
bool

True if the update was successful, false otherwise.

buildQuery()

Build a PDOStatement for querying the database based on the specified conditions.

private buildQuery(array<string, mixed> $where[, PDO|null $pdo = null ]) : PDOStatement
Parameters
$where : array<string, mixed>

An associative array of conditions for the query.

$pdo : PDO|null = null

The PDO instance for database connection (optional).

Tags
throws
UnhandledException

If an error occurs while preparing the PDOStatement.

Return values
PDOStatement

The prepared PDOStatement for the query.

Search results