Download the PHP package hstanleycrow/easyphpdbcore without Composer
On this page you can find all versions of the php package hstanleycrow/easyphpdbcore. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hstanleycrow/easyphpdbcore
More information about hstanleycrow/easyphpdbcore
Files in hstanleycrow/easyphpdbcore
Package easyphpdbcore
Short Description Lightweight PDO-based database layer with a simple CRUD model for PHP.
License MIT
Homepage https://github.com/hstanleycrow/EasyPHPDBCore
Informations about the package easyphpdbcore
English | Español
EasyPHPDBCore
Lightweight PDO-based database layer for PHP, with a simple CRUD Model and prepared statements everywhere.
Requirements
- PHP 8.2 or higher
- Composer
- PDO with the
pdo_mysqldriver (for the MySQL/MariaDB connection)
Runtime dependency: psr/log. A PSR-3 logger is optional; without one, errors are simply thrown as exceptions.
Installation
Quick example
Every write uses prepared statements with bound values, so array keys become
column names and array values become bound parameters. Never interpolate user
input into query() strings; pass it through the bindings instead.
Custom read queries
getRecords() accepts positional or named bindings:
Optional logging
Any PSR-3 logger can be injected as the
last constructor argument of the connection and the model (or the record
classes). When omitted, a NullLogger is used and failures are only thrown.
Error handling
All failures throw typed exceptions instead of printing anything:
hstanleycrow\EasyPHPDBCore\Exception\ConnectionException— connection/config errors.hstanleycrow\EasyPHPDBCore\Exception\QueryException— query execution errors.
Both extend hstanleycrow\EasyPHPDBCore\Exception\DatabaseException, so you can
catch either one specifically or the base class for all database errors.
Public API
Model
| Method | Description |
|---|---|
__construct(IConnection $connection, ?LoggerInterface $logger = null) |
Build a model. Subclasses set protected ?string $table. |
create(array $fieldsList): self |
Insert a row. Keys are columns, values are bound. |
lastInsertId(): ?int |
Id generated by the last create(). |
query(string $query): self |
Set a raw SELECT to run with getRecords(). |
getRecords(array $bindings = []): array |
Run the current query and return rows as associative arrays. |
getById(int\|string $id): ?array |
SELECT * by primary key; null if not found. |
update(array $updateFields, array $whereConditions): self |
Update rows matching all where conditions. |
delete(array $whereConditions): self |
Delete rows matching all where conditions. |
beginTransaction() / commit() / rollback(): void |
Transaction control on the underlying PDO. |
Connection
| Class | Description |
|---|---|
Connection\MySQLPDOConnection |
Opens a real PDO MySQL/MariaDB connection. |
Connection\MockConnection |
No-op connection for tests. |
Connection\MySQLEnvConfig / MySQLEnvCharsetConfig |
Read credentials/charset from an env array. |
Connection\IConnection / IConfig / ICharsetConfig |
Interfaces to plug in your own implementations. |
Record classes (used internally by Model, usable standalone)
CreateRecords, ReadRecords, UpdateRecords, DeleteRecords each expose an
execute(...) method and share the same (IConnection, string $table, ?LoggerInterface)
constructor shape (ReadRecords takes the query instead of a table).
Testing
The test suite runs against an in-memory SQLite database, so no MySQL server is required.
License
MIT — see LICENSE.