Search by

cycle / orm-promise-mapper

wolfy-j

Promise mapper for Cycle ORM

Package info

github.com/cycle/orm-promise-mapper

pkg:composer/cycle/orm-promise-mapper

Statistics

Installs: 1 854

Dependents: 1

Suggesters: 0

Stars: 2

Open Issues: 0

2.0.0 2026-11-21 22:22 UTC

This package is auto-updated.

Last update: 2026-11-22 10:15:30 UTC


README

CycleORM Logo

Promise mapper for Cycle ORM

Documentation Sponsor Discord

Psalm Level Type Coverage Codecov Mutation testing badge


With cycle/orm-promise-mapper, Cycle ORM hydrates entities as instances of your own classes and puts \Cycle\ORM\Reference\Promise objects into lazy-loaded relations.

Get Started

Requirements

  • PHP 8.2+

Installation

composer require cycle/orm-promise-mapper

PHP Latest Version on Packagist License Total Downloads

Define the Entity

use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Relation\BelongsTo;
use Cycle\Annotated\Annotation\Relation\HasMany;
use Cycle\ORM\PromiseMapper\PromiseMapper;
use Cycle\ORM\Reference\ReferenceInterface;

#[Entity(mapper: PromiseMapper::class)]
class User
{
    #[Column(type: 'primary')]
    public int $id;

    #[HasMany(target: Post::class, load: 'eager')]
    public array $posts;
    
    #[HasMany(target: Tag::class, load: 'lazy')]
    public ReferenceInterface|array $tags;
}

#[Entity(mapper: PromiseMapper::class)]
class Post
{
    // ...

    #[BelongsTo(target: User::class, load: 'lazy')]
    public ReferenceInterface|User $user;

    #[BelongsTo(target: Tag::class, load: 'eager')]
    public Tag $tag;
}

Fetching entity data

$user = $orm->getRepository('user')->findByPK(1);

// $user->posts contains an array because of eager loading
foreach ($user->posts as $post) {
    // ...
}

// $user->tags contains Cycle\ORM\Reference\Promise object because of lazy loading
$tags = $user->tags->fetch();
foreach ($tags as $tag) {
    // ...
}

$post = $orm->getRepository('post')->findByPK(1);

// $post->user contains Cycle\ORM\Reference\Promise object because of lazy loading
$userId = $post->user->fetch()->id;

// $post->tag contains Tag object because of eager loading
$tagName = $post->tag->name;

Testing

Tests run on Testo through the cycle/testing harness: tests/Unit needs no database, tests/Feature runs once per database driver. Unit and SQLite tests need no containers:

composer test:unit
composer test:sqlite

The other drivers run against the shared test databases:

composer db:up              # or: composer db:up -- mysql
composer test               # every suite; drivers whose server is down are skipped
composer test:mysql         # also test:postgres, test:sqlserver
composer db:down