Download the PHP package ez-php/orm without Composer
On this page you can find all versions of the php package ez-php/orm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package orm
Short Description ORM module for the ez-php framework — Active Record style models with a fluent query builder and schema builder
License MIT
Homepage https://github.com/ez-php/orm
Informations about the package orm
ez-php/orm
ORM module for the ez-php framework — Data Mapper pattern with Entity, AbstractRepository, a fluent QueryBuilder, and a Schema builder.
Requirements
- PHP 8.5+
- ext-pdo
- ez-php/framework 0.*
Installation
Setup
Register the service providers:
Usage
Defining an entity
Defining a repository
Persisting
Querying
Large result sets: chunk() pages with LIMIT/OFFSET. chunkById() and lazy() page by key
instead (WHERE id > last ORDER BY id) — every page costs the same, and rows the callback updates or
deletes do not make later pages skip or repeat rows. Both keep the query's where() clauses and the
soft-delete filter; the key column (default id) must be unique and selected:
Column names are validated and quoted in every clause. having() and select() take a plain column or one
aggregate over a column or * — COUNT, SUM, AVG, MIN, MAX, optionally DISTINCT (select() also
accepts AS alias). For any other SQL expression use selectRaw(), which is emitted verbatim — never pass user input to it:
Soft deletes
Relations
Define relation helpers on the repository, then call them on an entity. Each helper takes the related repository instance, the key columns and the owning entity's key value:
with() already loads a relation for a whole result set in one query. If you instead access
relations lazily inside a loop, RelationBatcher (requires ez-php/dataloader) collapses those
per-entity queries into one per relation:
hasOne(), hasMany() and belongsToMany() work the same way (hasMany/belongsToMany resolve to a
list, empty when there are none). belongsToMany() loads every queued owner through one pivot join:
Custom casts
Entity lifecycle observers
Attach observers to a repository to react to create/update/delete events:
The *ing hooks fire before the DB operation; *ed hooks fire after.
Append-only repositories
For insert-only tables (audit logs, event logs, immutable records) extend AppendOnlyRepository
instead of AbstractRepository. create() inserts and returns the entity with its generated
primary key; save() and delete() throw LogicException, so the constraint is enforced by the
type rather than by convention.
Typed attribute getters
Entity::getAttribute() returns mixed. Use the TypedAttributes trait for typed access that
satisfies PHPStan level 9 without per-call narrowing:
getInt()/getString() fall back to a default for non-numeric/non-scalar values;
getNullableDatetime() parses Y-m-d H:i:s and returns null otherwise.
Schema builder
Query logging
LoggingDatabase decorates any DatabaseInterface and logs SQL + bindings + duration for
every query()/execute() call via ez-php/logging (a soft dependency — install it
separately, since it's declared in require-dev here, not require):
Opt-in only — wrap the connection you pass in yourself; nothing logs by default.
Console commands
| Command | Description |
|---|---|
make:entity |
Scaffolds an Entity subclass in app/Entities/ |
make:repository |
Scaffolds an AbstractRepository subclass in app/Repositories/ |
Both are registered automatically by EntityServiceProvider::boot() when the application implements
CommandRegistryInterface (the ez-php Application does). They write relative to the working directory
(<cwd>/app/…) — run php ez from the project root.
Classes
| Class | Description |
|---|---|
Entity |
Abstract Data Mapper entity base; attributes, casts, fillable guards, relation storage |
AbstractRepository |
Abstract repository base; INSERT/UPDATE/DELETE, dirty tracking, relations, eager-load |
AppendOnlyRepository |
Repository base for insert-only tables (audit/event logs): create() inserts; save()/delete() throw LogicException |
TypedAttributes |
Trait adding getInt()/getString()/getBool()/getNullableInt()/getNullableString()/getNullableDatetime() to an entity |
EntityObserverInterface |
Lifecycle hook contract: creating/created/updating/updated/deleting/deleted |
ObservableRepositoryTrait |
Adds observer support to a repository; fires hooks around save() and delete() |
EntityQueryBuilder |
Typed fluent query builder for entities; with(), withCount(), paginate(), chunk()/chunkById()/lazy() |
EntityServiceProvider |
Calls Entity::setDatabase($db) in boot(); registers make:entity / make:repository |
Hydrator |
Converts raw DB rows → Entity instances and Entity attributes → storage arrays |
CastableInterface |
Interface for custom value-object casts: castFrom()/castTo() |
DuplicateKeyException |
Thrown on duplicate-key violations by QueryBuilder::insert()/insertBatch() and therefore by repository save() (original PDOException is getPrevious()) |
Paginator |
Immutable page-of-results value object |
QueryBuilder |
Fluent SQL builder for raw rows; all WHERE/JOIN/ORDER/LIMIT/aggregates/paginate/chunk/chunkById/lazy/cache |
LoggingDatabase |
DatabaseInterface decorator logging SQL + bindings + duration |
EntityHasMany |
One-to-many relation (FK on related entity) |
EntityHasOne |
One-to-one relation (FK on related entity) |
EntityBelongsTo |
Inverse of HasMany/HasOne (FK on owning entity) |
EntityBelongsToMany |
Many-to-many relation via pivot table |
Schema |
DDL façade: create(), table(), drop(), dropIfExists(), hasTable() |
Blueprint |
Column and constraint builder for CREATE TABLE and ALTER TABLE |
License
MIT — Andreas Uretschnig
All versions of orm with dependencies
ez-php/contracts Version ^2.7.0
ez-php/console Version ^2.7.0
ez-php/cache Version ^2.7.0