Indura API

BaseModel
in package

AbstractYes

Class to create CRUD models for PostgreSQL databases.

Abstract base class for PostgreSQL database models. Provides CRUD operations, validation, and field filtering functionality. Child classes should define table name, primary key, fillable fields, and validation rules.

Table of Contents

Properties

$schemeValidator  : Scheme
$connection  : PDO
$fillable  : mixed
$primaryKey  : int
$table  : string
$validationRules  : array<string|int, mixed>

Methods

__construct()  : mixed
Constructor
create()  : array<string|int, mixed>
Creates a new record in the table
delete()  : bool
Deletes a record from the table
findAll()  : array<string|int, mixed>
Retrieves all records from the table
findById()  : array<string|int, mixed>|false
Retrieves a single record by its primary key
update()  : array<string|int, mixed>
Updates an existing record
filterFillable()  : array<string|int, mixed>
Filters data to only include fillable fields
validate()  : void
Validates data against the model's validation rules

Properties

$validationRules

protected array<string|int, mixed> $validationRules = []

Methods

__construct()

Constructor

public __construct(PDO $connection) : mixed

Initializes the model with a database connection and sets up the validation scheme.

Parameters
$connection : PDO

Database connection instance

create()

Creates a new record in the table

public create(array<string|int, mixed> $data) : array<string|int, mixed>

Validates data, filters to fillable fields only, and inserts the record. Returns the newly created record including its generated primary key.

Parameters
$data : array<string|int, mixed>

Associative array of field names and values

Tags
throws
Exception

If validation fails or database operation fails

Return values
array<string|int, mixed>

The newly created record

delete()

Deletes a record from the table

public delete(int $id) : bool

Validates the record exists before attempting deletion.

Parameters
$id : int

The primary key value of the record to delete

Tags
throws
Exception

If record not found or database operation fails

Return values
bool

True if deletion was successful

findAll()

Retrieves all records from the table

public findAll() : array<string|int, mixed>

Fetches all records ordered by the primary key.

Tags
throws
Exception

If database query fails

Return values
array<string|int, mixed>

Array of records as associative arrays

findById()

Retrieves a single record by its primary key

public findById(int $id) : array<string|int, mixed>|false
Parameters
$id : int

The primary key value

Tags
throws
Exception

If database query fails

Return values
array<string|int, mixed>|false

Associative array of the record or false if not found

update()

Updates an existing record

public update(int $id, array<string|int, mixed> $data) : array<string|int, mixed>

Validates the record exists, validates data, filters to fillable fields, and updates the record. Returns the updated record.

Parameters
$id : int

The primary key value of the record to update

$data : array<string|int, mixed>

Associative array of field names and values to update

Tags
throws
Exception

If record not found, validation fails, or database operation fails

Return values
array<string|int, mixed>

The updated record

filterFillable()

Filters data to only include fillable fields

protected filterFillable(array<string|int, mixed> $data) : array<string|int, mixed>

Protects against mass-assignment vulnerabilities by only allowing fields defined in the $fillable property.

Parameters
$data : array<string|int, mixed>

Associative array of data to filter

Return values
array<string|int, mixed>

Filtered array containing only fillable fields

validate()

Validates data against the model's validation rules

protected validate(array<string|int, mixed> $data) : void

Uses the Scheme validator to validate data. If validation fails, sends a validation error response and halts execution.

Parameters
$data : array<string|int, mixed>

Associative array of data to validate

Tags
throws
void

Sends validation response and exits if validation fails


        
On this page

Search results