Skip to content
cyclePublic

About

PHP DataMapper, ORM

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1.3k stars

Watchers

26 watching

Forks

Repository files navigation

PHP DataMapper ORM and Data Modelling Engine

Documentation Sponsor Discord

Psalm Level Type Coverage Codecov Mutation testing badge


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.

Get Started

Installation

The package requires PHP 8.2+.

composer require cycle/orm

PHP Latest Version on Packagist License Total Downloads

See the documentation to configure the database connection and the mapping schema.

Example

// 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();

Agent skills

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/skills

Entity mapping with PHP attributes is covered by the cycle-orm-attributes skill that comes with cycle/annotated.

Features

  • 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

Testing

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:down

composer 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.

Extensions

Component Description Current Status
cycle/database Database abstraction layer, query builders and schema introspection License PHP Stable Release Total Downloads
cycle/schema-builder Compiles entity declarations into the ORM schema and syncs tables License PHP Stable Release Total Downloads
cycle/annotated Entities, columns and relations declared with PHP attributes License PHP Stable Release Total Downloads
cycle/schema-provider Builds the ORM schema from different sources, with caching License PHP Stable Release Total Downloads
cycle/schema-renderer Renders the schema as PHP code, console output or Mermaid License PHP Stable Release Total Downloads
cycle/migrations Migration runner and repository License PHP Stable Release Total Downloads
cycle/schema-migrations-generator Generates migrations from the difference between the ORM schema and the database License PHP Stable Release Total Downloads
cycle/entity-behavior Auto timestamps, soft deletes, optimistic locking, hooks and event listeners License PHP Stable Release Total Downloads
cycle/entity-behavior-uuid UUID columns and primary keys License PHP Stable Release Total Downloads
cycle/entity-behavior-identifier ramsey/identifier types as entity columns License PHP Stable Release Total Downloads
cycle/transaction Raw queries and a scoped Entity Manager in one database transaction License PHP Stable Release Total Downloads
cycle/active-record The Active Record pattern on top of Cycle ORM License PHP Stable Release Total Downloads
cycle/orm-promise-mapper Hydrates your own classes with promises in lazy relations License PHP Stable Release Total Downloads

About

PHP DataMapper, ORM

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1.3k stars

Watchers

26 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages