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.
composer require solophp/repositoryRequires PHP 8.3+, Doctrine DBAL ^4.6 and the PDO driver of your database. Tested on MySQL 8.4, MariaDB 12.3, PostgreSQL 17 and SQLite.
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Types\Types;
use Solo\Repository\Mapping;
use Solo\Repository\PageRequest;
use Solo\Repository\Repository;
final readonly class Product
{
public function __construct(public int $id, public string $sku, public string $price)
{
}
}
$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
)');
// The constructor gives the columns and builds the model; only the decimal is named.
$products = new Repository($db, Mapping::of(Product::class, table: 'products', types: ['price' => Types::DECIMAL]));
$id = $products->insertGetId(['sku' => 'CHAIR', 'price' => '49.90']);
$products->update($id, ['price' => '59.90']);
$product = $products->read()->find($id); // Product|null
$page = $products->read()->page(new PageRequest(1, 20, ['price' => 'ASC']), ['price >=' => '50.00']);
// $page->items, and $page->total of every matching productMapping::of() reads each constructor parameter as the column of its name, typed by its PHP type (int, string, bool, float, array as JSON, dates and backed enums), and builds the model from values already converted by DBAL. Only a column whose type the default misreads, such as a date, decimal or binary one, is named in types. A model built another way gets a hand-written mapping: new Mapping(table: 'products', columns: [...], hydrate: Product::fromRow(...)). 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.
- Getting Started
- Typed Repositories
- Mapping and Types
- Read Queries
- Pages
- Criteria and Filter
- LIKE Patterns
- Translations
- Relations
- Writes
- Transactions and DBAL
- Behavior in Detail
- Changelog
MIT. See LICENSE.