PHP DataMapper ORM and Data Modelling Engine
Cycle is PHP DataMapper, ORM and Data Modelling engine designed to safely work in classic and daemonized PHP applications (like RoadRunner). The ORM provides flexible configuration options to model datasets, powerful query builder and supports dynamic mapping schema. The engine can work with plain PHP objects, support annotation declarations, and proxies via extensions.
The package requires PHP 8.2+.
composer require cycle/ormSee the documentation to configure the database connection and the mapping schema.
// load all active users and pre-load their paid orders sorted from newest to oldest
// the pre-load will be complete using LEFT JOIN
$users = $orm->getRepository(User::class)
->select()
->where('active', true)
->load('orders', [
'method' => Select::SINGLE_QUERY,
'load' => function($q) {
$q->where('paid', true)->orderBy('timeCreated', 'DESC');
}
])
->fetchAll();
$em = new EntityManager($orm);
foreach($users as $user) {
$em->persist($user);
}
$em->run();Cycle ORM comes with the cycle-orm Agent Skill from cycle/skills: it teaches coding agents such as Claude Code and Codex to work with the ORM runtime — bootstrap the ORM, query through repositories and Select, save entities with the EntityManager, and write custom repositories, scopes, mappers and typecasters. Install the llm/skills Composer plugin, and it copies the skill into .agents/skills/ on every composer install and update:
composer require --dev llm/skillsEntity mapping with PHP attributes is covered by the cycle-orm-attributes skill that comes with cycle/annotated.
- ORM with has-one, has-many, many-through-many and polymorphic relations
- Plain Old PHP objects, AR, Custom objects or same entity type for multiple repositories
- eager and lazy loading, query builder with multiple fetch strategies
- embedded entities, lazy/eager loaded embedded partials
- runtime configuration with/without code-generation
- column-to-field mapping, single table inheritance, value objects support
- hackable: persist strategies, mappers, relations, transactions
- works with directed graphs and cyclic graphs using command chains
- designed to work in long-running applications: immutable service core, disposable UoW
- supports MySQL, MariaDB, PostgreSQL, SQL Server, SQLite
- schema scaffolding, introspection, migrations and debugging
- supports global query scopes, UUIDs as PK, soft deletes, auto timestamps and macros
- custom column types, FKs to non-primary columns
- use with or without annotations, proxy classes, and auto-migrations
- compatible with Doctrine Collections, Illuminate Collections and custom collections
- compatible with Doctrine Annotations, PHP8 attributes
The tests run on Testo through the shared cycle/testing harness:
tests/Unit needs no database, tests/Feature and tests/Regression (one folder per reported issue) run once per database driver.
composer test:unit # no database
composer test:sqlite # in-memory SQLite, no docker
composer db:up # MySQL, Postgres and SQL Server in docker
composer test # every suite; a driver whose server is down is skipped
composer db:downcomposer test:mysql, test:postgres and test:sqlserver run one driver. To reproduce a new issue, generate a case:
composer test:new-case -- 123 writes tests/Regression/Issue123.
| Component | Description | Current Status |
|---|---|---|
| cycle/database | Database abstraction layer, query builders and schema introspection | |
| cycle/schema-builder | Compiles entity declarations into the ORM schema and syncs tables | |
| cycle/annotated | Entities, columns and relations declared with PHP attributes | |
| cycle/schema-provider | Builds the ORM schema from different sources, with caching | |
| cycle/schema-renderer | Renders the schema as PHP code, console output or Mermaid | |
| cycle/migrations | Migration runner and repository | |
| cycle/schema-migrations-generator | Generates migrations from the difference between the ORM schema and the database | |
| cycle/entity-behavior | Auto timestamps, soft deletes, optimistic locking, hooks and event listeners | |
| cycle/entity-behavior-uuid | UUID columns and primary keys | |
| cycle/entity-behavior-identifier | ramsey/identifier types as entity columns | |
| cycle/transaction | Raw queries and a scoped Entity Manager in one database transaction | |
| cycle/active-record | The Active Record pattern on top of Cycle ORM | |
| cycle/orm-promise-mapper | Hydrates your own classes with promises in lazy relations |