Skip to content

About

Typed PHP repositories over Doctrine DBAL with translation fallback, batch-loaded relations, stable pagination, and transactions.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Solo Repository

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.

About

Typed PHP repositories over Doctrine DBAL with translation fallback, batch-loaded relations, stable pagination, and transactions.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages