https://github.com/cycle/orm-promise-mapper
Promise mapper
https://github.com/cycle/orm-promise-mapper
Last synced: 10 months ago
JSON representation
Promise mapper
- Host: GitHub
- URL: https://github.com/cycle/orm-promise-mapper
- Owner: cycle
- License: mit
- Created: 2021-12-15T14:35:56.000Z (over 4 years ago)
- Default Branch: 1.x
- Last Pushed: 2025-05-02T11:29:00.000Z (about 1 year ago)
- Last Synced: 2025-08-27T20:42:49.201Z (10 months ago)
- Language: PHP
- Size: 78.1 KB
- Stars: 3
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Cycle ORM PromiseMapper
[](https://packagist.org/packages/cycle/orm-promise-mapper)
[](https://github.com/cycle/orm-promise-mapper/actions)
[](https://scrutinizer-ci.com/g/cycle/orm-promise-mapper/?branch=1.x)
[](https://codecov.io/gh/cycle/orm-promise-mapper)

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](https://getcomposer.org/download/):
```bash
composer require cycle/orm-promise-mapper
```
## Define the Entity
```php
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
```php
$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`](./LICENSE) for more information.
Maintained by [Spiral Scout](https://spiralscout.com).