solophp / abstract-repository
Base repository for Solo Query Builder — extendable, minimal, and typed.
v1.0.0
2025-04-02 15:20 UTC
Requires
- php: >=8.2
- solophp/query-builder: ^1.4
README
A lightweight abstract repository layer for use with Solo Query Builder. Provides a clean, extensible interface for working with database tables using a reusable base class.
📥 Installation
composer require solophp/abstract-repository
Requires
solophp/query-builder
as a dependency.
✨ Features
- Fully typed and extensible abstract repository
- Built-in support for:
- Select by ID or criteria
- Insert/update/delete (with batch insert)
- Existence checks
- Soft delete via
deleted_at
- Restore and select deleted records
- Sync data by primary key
🚀 Quick Example
use Solo\Repository\AbstractRepository; class UserRepository extends AbstractRepository { protected bool $softDeletes = true; public function __construct(QueryBuilder $qb) { parent::__construct($qb, 'users'); } } $repo = new UserRepository($qb); $repo->insert(['name' => 'Neo']); $user = $repo->selectById(1); $repo->deleteById(1); // Soft delete (sets deleted_at) $repo->restoreById(1); // Restores soft-deleted row $repo->forceDeleteById(1); // Force delete (real DELETE)
🧰 Available Methods
Method | Description |
---|---|
selectById($id) |
Fetch single record by primary key |
selectAll() |
Fetch all records |
selectFirstBy($criteria) |
Fetch first matching row |
selectBy($criteria) |
Fetch records with optional sorting and pagination |
insert($data) |
Insert a single row |
insertMultiple($rows) |
Insert many rows in a transaction |
update($id, $data) |
Update a record by ID |
deleteById($id) |
Soft delete (or hard if disabled) |
forceDeleteById($id) |
Always delete physically |
deleteBy($criteria) |
Delete matching records |
exists($criteria) |
Check if record exists |
existsExcluding($criteria, $id) |
Check existence excluding an ID |
restoreById($id) |
Restore soft-deleted row |
selectDeleted() |
Get all soft-deleted rows |
syncById($new, $current) |
Sync rows by primary key (insert/update/delete) |
🧱 Requirements
- PHP 8.2+
solophp/query-builder
⚖️ License
MIT — LICENSE