cycle/orm-promise-mapper

1.0.0 2021-12-22 17:13 UTC

This package is auto-updated.

Last update: 2022-05-10 14:49:25 UTC


README

Latest Stable Version Build Status Scrutinizer Code Quality Codecov 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646973636f72642d636861742d6d6167656e74612e737667

Cycle ORM provides the ability to carry data over the specific class instances by using cycle/orm-promise-mapper package with \Cycle\ORM\Reference\Promise objects for relations with lazy loading.

Installation

The preferred way to install this package is through Composer:

composer require cycle/orm-promise-mapper

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\Reference\ReferenceInterface;

#[Entity]
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]
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 $post) {
    // ...
}

$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;

License:

The MIT License (MIT). Please see LICENSE for more information. Maintained by Spiral Scout.