Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Initial release of **Solo Repository** (`solophp/repository`) with the

- Explicit, typed `Mapping` and `Repository`; constructing repositories and
queries never opens a database connection. An application can extend
`Repository` into a typed repository per table; its public methods are final.
`Repository` into a typed repository per table; its public methods are final,
and minor releases may add new ones.
- Reusable `ReadQuery` with projection reads, `sum`/`min`/`max`/`avg` aggregates
over the same criteria as `count`, bounded integer-ID iteration, and
`queryBuilder()`, a DBAL `QueryBuilder` over the rows its criteria select.
Expand Down Expand Up @@ -37,6 +38,32 @@ Initial release of **Solo Repository** (`solophp/repository`) with the
`LinkSet` operations.
- SQLite tests, MySQL 8.4/MariaDB 12.3/PostgreSQL 17 integration tests, and a PHP/DBAL CI matrix.

### Changes since 1.0.0-RC5

- The compatibility rules for subclasses of `Repository` are documented.
Throughout 1.x it stays a non-final `readonly` class whose public methods,
except the constructor, are final and whose state is private. A minor release
may add public methods, and a subclass that declares a method of the same
name then fails to load. Constructor parameters keep their names and order;
a parameter added in a minor release is optional and comes last.
- **Behavior change:** `LinkSet::attach()` and `sync()` skip a pair that
another writer stored after the operation read the set, instead of failing
on the pivot's unique key. Their INSERT carries `ON CONFLICT (pair) DO NOTHING`
on PostgreSQL and SQLite and `ON DUPLICATE KEY UPDATE` on MySQL and MariaDB;
foreign-key violations still propagate. Concurrent attaches of one pair now
both succeed on MySQL and on PostgreSQL in READ COMMITTED. MariaDB with
snapshot isolation (error 1020) and PostgreSQL in REPEATABLE READ or
SERIALIZABLE (a serialization failure) still refuse a transaction whose
snapshot is older than the other writer's commit, and an exact final set
still needs an owner lock. On PostgreSQL and SQLite these writes now fail on a
pivot without the UNIQUE pair that the guide already required.
- A locking read on MySQL tells `FOR UPDATE OF` support by the server version,
8.0.1 or later, instead of by DBAL's `MySQL80Platform`. DBAL deprecates its
MySQL 8 platform classes and drops them with MySQL 5.7 support, after which
`MySQLPlatform` itself stands for MySQL 8; the lock keeps covering only the
read's table there. MySQL 5.7 and MariaDB keep the plain `FOR UPDATE`. The
package no longer refers to a deprecated DBAL platform class.

### Changes since 1.0.0-RC4

- `ReadQuery::forUpdate()` returns the same read as a locking one: `find()`,
Expand Down
14 changes: 10 additions & 4 deletions docs/guide/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This reference describes Solo Repository 1.0: PHP 8.3+, Doctrine DBAL `^4.3`, an

`Repository<T>`, `Mapping<T>` and `ReadQuery<T>` use PHPDoc generics, where `T` is an object. `T` is covariant, so `Repository<Product>` is accepted where `Repository<object>` is expected. Relation definitions are generic over their owner and target models, so static analysis checks every `assign` callback against the target mapping. The runtime checks that hydration and relation assignment return objects; it does not enforce their concrete class. `Mapping`, `ReadQuery`, the configuration objects and the write helpers are `final readonly`; `Repository` is `readonly` and may be extended into a [typed repository](#typed-repositories). Readonly configuration does not make the shared DBAL connection, returned models or objects captured by callbacks immutable.

The public API consists of the types and methods documented below. The `Internal` namespace and members marked `@internal` are implementation details without a semantic-versioning compatibility promise: the constructors of `ReadQuery`, `LinkSet` and `TranslationStore` (obtain them from a `Repository`), the properties of `Filter`, and `Relations\Relation::resolve()`. The abstract `Relation` base is not an extension point: use the four supplied final definitions. The legacy `BaseRepository`, `RepositoryInterface` and singular `Relation` namespace belong to the separate `solophp/base-repository` package; see the [migration guide](migration.md).
The public API consists of the types and methods documented below. The `Internal` namespace and members marked `@internal` are implementation details without a semantic-versioning compatibility promise: the constructors of `ReadQuery`, `LinkSet` and `TranslationStore` (obtain them from a `Repository`), the properties of `Filter`, and `Relations\Relation::resolve()`. The abstract `Relation` base is not an extension point: use the four supplied final definitions. `Repository` may be extended under the compatibility rules of [typed repositories](#typed-repositories). The legacy `BaseRepository`, `RepositoryInterface` and singular `Relation` namespace belong to the separate `solophp/base-repository` package; see the [migration guide](migration.md).

## A minimal repository

Expand Down Expand Up @@ -81,6 +81,12 @@ $product = (new ProductRepository($db))->read()->find($id); // Product|null

A `readonly` class can only be extended by a `readonly` class. A static `mapping()` also gives relations a target factory: `target: ProductRepository::mapping(...)`.

Subclasses can rely on these rules throughout 1.x:

- `Repository` stays a non-final `readonly` class. Its public methods, except the constructor, stay final, and its state stays private, so a subclass depends only on the public API.
- A minor release may add public methods to `Repository`. PHP refuses to load a subclass that declares a method of the same name, so name a subclass's methods after its table's domain, such as `cheaperThan()`, and read the changelog before upgrading.
- The constructor keeps the names, order and meaning of its parameters. A parameter added in a minor release is optional and comes last.

## Mapping and types

```php
Expand Down Expand Up @@ -180,7 +186,7 @@ In `findBy()`, `findOneBy()` and `rows()`, an `orderBy` key may also be a dotted
- `count()` and `exists()` do not hydrate models or resolve/load includes. Relation predicates in their criteria still apply. `exists()` selects a constant with a one-row limit.
- `sum()`, `min()`, `max()` and `avg()` aggregate one readable field, including a translated field under the query locale, over the rows `count()` would count. `sum()`, `min()` and `max()` convert the result through the field's DBAL type, so a decimal sum is an exact string and `max('created_at')` a `DateTimeImmutable`. `sum()` returns zero of that type when nothing matches; `min()`, `max()` and `avg()` return NULL. `avg()` returns a float. `sum()` and `avg()` require a smallint, integer, bigint, decimal, float or smallfloat field and reject other fields before SQL.
- `queryBuilder()` returns a new DBAL `QueryBuilder` over the rows `count()` would count, for SQL the other methods cannot express; see [the DBAL escape hatch](#transactions-and-the-dbal-escape-hatch). It rejects nonempty includes.
- `forUpdate()` returns the same read as a locking one: `find()`, `findOneBy()`, `findBy()`, `rows()`, `exists()` and every batch of `iterateById()` run `SELECT ... FOR UPDATE` and lock the rows they read until the surrounding transaction ends; outside a transaction they throw `LogicException`. Unlike a plain read in a REPEATABLE READ transaction, which answers from the transaction's snapshot, a locking read never returns an older version of a row: MySQL reads the latest committed row, while PostgreSQL and MariaDB (with `innodb_snapshot_isolation`, its default since 11.6) reject a row changed since the snapshot with a serialization failure, after which the transaction must start over. The lock covers rows of the read's own table: PostgreSQL and MySQL 8 lock with `FOR UPDATE OF`, while MariaDB, which lacks it, also locks the translation and order rows the statement joins. Relation loads read without locks. Aggregates and `queryBuilder()` of a locking read throw `LogicException`. SQLite has no row locks and runs the plain SELECT. Rows lock in the order the database reads them; where transactions lock overlapping sets of rows, read them in key order or take `lockForUpdate()` first. MySQL and MariaDB lock every row they read to answer the statement, so with an order no index serves, such as a relation path, a limited locking read locks all matching rows, not just the page.
- `forUpdate()` returns the same read as a locking one: `find()`, `findOneBy()`, `findBy()`, `rows()`, `exists()` and every batch of `iterateById()` run `SELECT ... FOR UPDATE` and lock the rows they read until the surrounding transaction ends; outside a transaction they throw `LogicException`. Unlike a plain read in a REPEATABLE READ transaction, which answers from the transaction's snapshot, a locking read never returns an older version of a row: MySQL reads the latest committed row, while PostgreSQL and MariaDB (with `innodb_snapshot_isolation`, its default since 11.6) reject a row changed since the snapshot with a serialization failure, after which the transaction must start over. The lock covers rows of the read's own table: PostgreSQL and MySQL from 8.0.1, which is told by the server version, lock with `FOR UPDATE OF`, while MariaDB and MySQL 5.7, which lack it, also lock the translation and order rows the statement joins. Relation loads read without locks. Aggregates and `queryBuilder()` of a locking read throw `LogicException`. SQLite has no row locks and runs the plain SELECT. Rows lock in the order the database reads them; where transactions lock overlapping sets of rows, read them in key order or take `lockForUpdate()` first. MySQL and MariaDB lock every row they read to answer the statement, so with an order no index serves, such as a relation path, a limited locking read locks all matching rows, not just the page.
- Without `orderBy`, ordering is unspecified. Add the primary key as a final tie-breaker for stable pages. Negative limit/offset values are rejected. `limit: 0` validates query settings and returns an empty list without executing the read.

```php
Expand Down Expand Up @@ -434,9 +440,9 @@ The relation must be `BelongsToMany`. Owner and target IDs are normalized using
- `detach()` removes the specified links; absent links are unchanged.
- `sync()` makes the physical set exactly match the supplied IDs; `sync($id, [])` removes all links for that owner.

Empty attach/detach lists execute no SQL after ID validation. Existing-link attach is a no-op in state, but can perform reads. These operations use physical pivot rows, including soft-deleted owners/targets; read visibility and Load criteria do not apply. String keys are resolved through SQL so comparisons respect the database's collation. Foreign-key and unique violations propagate, and orphan cleanup for a schema without the required FKs is not promised.
Empty attach/detach lists execute no SQL after ID validation. Existing-link attach is a no-op in state, but can perform reads. These operations use physical pivot rows, including soft-deleted owners/targets; read visibility and Load criteria do not apply. String keys are resolved through SQL so comparisons respect the database's collation. Foreign-key violations propagate, and orphan cleanup for a schema without the required FKs is not promised. A pair that another writer stored after the operation read the set is skipped rather than raised as a unique violation: the INSERT carries `ON CONFLICT (foreignPivotKey, relatedPivotKey) DO NOTHING` on PostgreSQL and SQLite, which therefore need the required UNIQUE pair, and `ON DUPLICATE KEY UPDATE` on MySQL and MariaDB, which treat a conflict over any unique index of the pivot as that pair.

All nonempty attach/detach operations and every sync use a transaction/savepoint and split large statements. This provides rollback, not serialization of concurrent edits to the same set. Coordinate concurrent writers with an application lock or transaction strategy. No automatic retries or blanket `INSERT IGNORE` are used.
All nonempty attach/detach operations and every sync use a transaction/savepoint and split large statements. This provides rollback, not serialization of concurrent edits to the same set. Concurrent attaches of one pair both succeed on MySQL and on PostgreSQL in READ COMMITTED. A transaction whose snapshot is older than the other writer's commit is refused instead by MariaDB with `innodb_snapshot_isolation` (its default since 11.6, error 1020) and by PostgreSQL in REPEATABLE READ or SERIALIZABLE (a serialization failure), and two syncs of one set can still interleave. Writers that must agree on a final set need an application lock, such as `lockForUpdate()` of the owner row as the first statement of their transaction. No automatic retries or blanket `INSERT IGNORE` are used: only the conflict over the pair itself is skipped.

## Transactions and the DBAL escape hatch

Expand Down
Loading
Loading