Search by

solophp / repository

Vitaliy Olos

Typed PHP repositories with consistent translation fallback and batch-loaded relations

Package info

github.com/SoloPHP/Repository

pkg:composer/solophp/repository

Statistics

Installs: 81

Dependents: 3

Suggesters: 0

Stars: 1

Open Issues: 0

v1.3.0 2026-11-01 18:16 UTC

This package is auto-updated.

Last update: 2026-11-01 18:18:20 UTC


README

Latest Version on Packagist PHP Version License

Typed PHP repositories on Doctrine DBAL. Map a table to your own objects, read through reusable queries, write explicitly, and get translations with one fallback rule and relations loaded in batches.

Installation

composer require solophp/repository

Requires PHP 8.3+, Doctrine DBAL ^4.3 and the PDO driver of your database. Tested on MySQL 8.4, MariaDB 12.3, PostgreSQL 17 and SQLite.

Example

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Types\Types;
use Solo\Repository\Mapping;
use Solo\Repository\PageRequest;
use Solo\Repository\Repository;

$db = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);
$db->executeStatement('CREATE TABLE products (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    sku TEXT NOT NULL UNIQUE,
    price DECIMAL(10, 2) NOT NULL
)');

/** @var Repository<stdClass> $products */
$products = new Repository($db, new Mapping(
    table: 'products',
    columns: ['id' => Types::INTEGER, 'sku' => Types::STRING, 'price' => Types::DECIMAL],
    hydrate: static fn(array $row): stdClass => (object) $row,
));

$id = $products->insertGetId(['sku' => 'CHAIR', 'price' => '49.90']);
$products->update($id, ['price' => '59.90']);

$product = $products->read()->find($id);
$page = $products->read()->page(new PageRequest(1, 20, ['price' => 'ASC']), ['price >=' => '50.00']);
// $page->items, and $page->total of every matching product

hydrate receives values already converted by their DBAL types and may build an immutable model of your own. The columns of the mapping are the only fields a read or a write accepts.

For an operation spanning repositories on the same connection, inject a Solo\Repository\TransactionManager constructed with that connection and call run(). Services choose the operation's boundary without receiving SQL access; DBAL handles commit, rollback and nested savepoints. See Transactions and DBAL.

Documentation

Full Documentation

License

MIT. See LICENSE.