cycle/annotated
Composer 安装命令:
composer require cycle/annotated
包简介
Cycle ORM Annotated Entities generator
README 文档
README
The package provides the ability to define Cycle ORM schema using PHP attributes.
Usage
use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\Column; #[Entity] class User { #[Column(type: 'primary')] private int $id; #[Column(type: 'string(32)')] private string $login; #[Column(type: 'enum(active,disabled)')] private string $status; #[Column(type: 'decimal(5,5)')] private $balance; }
Relations
HasOne
use Cycle\Annotated\Annotation\Relation\HasOne; use Cycle\Annotated\Annotation\Entity; #[Entity] class User { // ... #[HasOne(target: Address::class)] public ?Address $address; }
Note Read more about HasOne.
HasMany
use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\Relation\HasMany; #[Entity] class User { // ... #[HasMany(target: Post::class)] private array $posts; }
Note Read more about HasMany.
BelongsTo
use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\Relation\BelongsTo; #[Entity] class Post { // ... #[BelongsTo(target: User::class)] private User $user; }
Note Read more about BelongsTo.
RefersTo
use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\Relation\RefersTo; use Cycle\Annotated\Annotation\Relation\HasMany; #[Entity] class User { // ... #[RefersTo(target: Comment::class)] private ?Comment $lastComment; #[HasMany(target: Comment::class)] public array $comments; // ... public function addComment(Comment $c): void { $this->lastComment = $c; $this->comments[] = $c; } public function removeLastComment(): void { $this->lastComment = null; } public function getLastComment(): ?Comment { return $this->lastComment; } }
Note Read more about RefersTo.
ManyToMany
use Cycle\Annotated\Annotation\Relation\ManyToMany; use Cycle\Annotated\Annotation\Entity; #[Entity] class User { // ... #[ManyToMany(target: Tag::class, through: UserTag::class)] protected array $tags; public function getTags(): array { return $this->tags; } public function addTag(Tag $tag): void { $this->tags[] = $tag; } public function removeTag(Tag $tag): void { $this->tags = array_filter($this->tags, static fn(Tag $t) => $t !== $tag); } }
Note Read more about ManyToMany.
Embedded Entities
use Cycle\Annotated\Annotation\Embeddable; use Cycle\Annotated\Annotation\Column; use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\Relation\Embedded; #[Embeddable] class UserCredentials { #[Column(type: 'string(255)')] public string $username; #[Column(type: 'string')] public string $password; } #[Entity] class User { #[Column(type: 'primary')] public int $id; #[Embedded(target: 'UserCredentials')] public UserCredentials $credentials; public function __construct() { $this->credentials = new UserCredentials(); } }
Note Read more about Embedded Entities.
BelongsToMorphed
use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\Relation\Morphed\BelongsToMorphed; #[Entity] class Image { // ... #[BelongsToMorphed(taget: ImageHolderInterface::class)] public ImageHolderInterface $imageHolder; }
Note Read more about BelongsToMorphed.
MorphedHasOne
use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\Relation\Morphed\MorphedHasOne; #[Entity] class User { // ... #[MorphedHasOne(target: Image::class)] public $image; }
Note Read more about MorphedHasOne.
MorphedHasMany
use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\Relation\Morphed\MorphedHasMany; #[Entity] class User { // ... #[MorphedHasMany(target: Image::class)] public $images; }
Note Read more about MorphedHasMany.
Single Table Inheritance
#[Entity] #[DiscriminatorColumn(name: 'type')] // Discriminator column (required) class Person { #[Column(type: 'primary', primary: true)] protected int $id; #[Column(type: 'string')] protected string $name; } #[Entity] #[InheritanceSingleTable] class Employee extends Person { #[Column(type: 'int')] protected int $salary; } #[Entity] #[InheritanceSingleTable(value: 'foo_customer')] class Customer extends Person { #[Column(type: 'json')] protected array $preferences; }
Note Read more about Single Table Inheritance.
Joined Table Inheritance
#[Entity] class Person { #[Column(primary: true)] protected int $id; #[Column()] protected int $fooId; #[Column(type: 'string')] protected string $name; } #[Entity] #[InheritanceJoinedTable(outerKey: 'fooId')] class Employee extends Person { #[Column(type: 'int')] protected int $salary; } #[Entity] #[InheritanceJoinedTable(outerKey: 'id')] class Customer extends Person { #[Column(type: 'json')] protected array $preferences; }
Note Read more about Joined Table Inheritance.
License
The MIT License (MIT). Please see LICENSE for more information. Maintained
by Spiral Scout.
cycle/annotated 适用场景与选型建议
cycle/annotated 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 781.28k 次下载、GitHub Stars 达 28, 最近一次更新时间为 2019 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 cycle/annotated 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cycle/annotated 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 781.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 20
- 依赖项目数: 56
- 推荐数: 3
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-29