cycle / orm-promise-mapper
1.0.1
2023-11-21 18:13 UTC
Requires
- php: >=8.0
- cycle/orm: ^2.0
- doctrine/instantiator: ^1.3.1 || ^2.0
- laminas/laminas-hydrator: ^4.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- spiral/tokenizer: ^2.8
- vimeo/psalm: ^4.13
README
Cycle ORM 通过使用 cycle/orm-promise-mapper
包和 \Cycle\ORM\Reference\Promise
对象来实现关系的懒加载,提供了在特定类实例间携带数据的能力。
安装
安装此包的首选方式是通过 Composer
composer require cycle/orm-promise-mapper
定义实体
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; }
获取实体数据
$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;
许可证
MIT 许可证 (MIT)。更多信息请参阅 LICENSE
。由 Spiral Scout 维护。