{"id":13579237,"url":"https://github.com/cycle/annotated","last_synced_at":"2025-04-05T00:06:44.800Z","repository":{"id":37601652,"uuid":"177779947","full_name":"cycle/annotated","owner":"cycle","description":"Schema generation using annotated entities and mappers","archived":false,"fork":false,"pushed_at":"2025-01-10T08:07:57.000Z","size":608,"stargazers_count":25,"open_issues_count":12,"forks_count":15,"subscribers_count":3,"default_branch":"4.x","last_synced_at":"2025-03-28T23:04:57.885Z","etag":null,"topics":["attributes","cycle","datamapper","hacktoberfest","orm","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cycle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"cycle"}},"created_at":"2019-03-26T12:01:44.000Z","updated_at":"2025-02-06T14:11:56.000Z","dependencies_parsed_at":"2024-08-01T15:30:26.141Z","dependency_job_id":"25424056-5fef-4b32-90d3-ef1f26621c18","html_url":"https://github.com/cycle/annotated","commit_stats":{"total_commits":162,"total_committers":12,"mean_commits":13.5,"dds":0.537037037037037,"last_synced_commit":"44cdb427ffea784c4a5fe5308892229d1d20fda0"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cycle%2Fannotated","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cycle%2Fannotated/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cycle%2Fannotated/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cycle%2Fannotated/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cycle","download_url":"https://codeload.github.com/cycle/annotated/tar.gz/refs/heads/4.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266563,"owners_count":20910836,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attributes","cycle","datamapper","hacktoberfest","orm","php"],"created_at":"2024-08-01T15:01:37.619Z","updated_at":"2025-04-05T00:06:44.783Z","avatar_url":"https://github.com/cycle.png","language":"PHP","readme":"# Cycle ORM - Annotated Entities\n\n[![PHP Version Require](https://poser.pugx.org/cycle/annotated/require/php)](https://packagist.org/packages/cycle/annotated)\n[![Latest Stable Version](https://poser.pugx.org/cycle/annotated/v/stable)](https://packagist.org/packages/cycle/annotated)\n[![phpunit](https://github.com/cycle/annotated/actions/workflows/main.yml/badge.svg)](https://github.com/cycle/annotated/actions)\n[![psalm](https://github.com/cycle/annotated/actions/workflows/psalm.yml/badge.svg)](https://github.com/cycle/annotated/actions)\n[![psalm-level](https://shepherd.dev/github/cycle/annotated/level.svg)](https://shepherd.dev/github/cycle/annotated)\n[![Codecov](https://codecov.io/gh/cycle/annotated/branch/4.x/graph/badge.svg)](https://codecov.io/gh/cycle/annotated/)\n[![Total Downloads](https://poser.pugx.org/cycle/annotated/downloads)](https://packagist.org/packages/cycle/annotated)\n\u003ca href=\"https://discord.gg/8bZsjYhVVk\"\u003e\u003cimg src=\"https://img.shields.io/badge/discord-chat-magenta.svg\"\u003e\u003c/a\u003e\n\n\u003cb\u003e[Documentation](https://cycle-orm.dev/docs/annotated-prerequisites)\u003c/b\u003e | [Cycle ORM](https://github.com/cycle/orm)\n\nThe package provides the ability to define Cycle ORM schema using PHP attributes.\n\n## Usage\n\n```php\nuse Cycle\\Annotated\\Annotation\\Entity;\nuse Cycle\\Annotated\\Annotation\\Column;\n\n#[Entity]\nclass User\n{\n    #[Column(type: 'primary')]\n    private int $id;\n\n    #[Column(type: 'string(32)')]\n    private string $login;\n\n    #[Column(type: 'enum(active,disabled)')]\n    private string $status;\n\n    #[Column(type: 'decimal(5,5)')]\n    private $balance;\n}\n```\n\n### Relations\n\n#### HasOne\n\n```php\nuse Cycle\\Annotated\\Annotation\\Relation\\HasOne;\nuse Cycle\\Annotated\\Annotation\\Entity;\n\n#[Entity]\nclass User\n{\n    // ...\n\n    #[HasOne(target: Address::class)]\n    public ?Address $address;\n}\n\n```\n\n\u003e **Note**\n\u003e Read more about [HasOne](https://cycle-orm.dev/docs/relation-has-one).\n\n#### HasMany\n\n```php\nuse Cycle\\Annotated\\Annotation\\Entity;\nuse Cycle\\Annotated\\Annotation\\Relation\\HasMany;\n\n#[Entity]\nclass User\n{\n    // ...\n\n    #[HasMany(target: Post::class)]\n    private array $posts;\n}\n```\n\n\u003e **Note**\n\u003e Read more about [HasMany](https://cycle-orm.dev/docs/relation-has-many).\n\n#### BelongsTo\n\n```php\nuse Cycle\\Annotated\\Annotation\\Entity;\nuse Cycle\\Annotated\\Annotation\\Relation\\BelongsTo;\n\n#[Entity]\nclass Post\n{\n    // ...\n\n    #[BelongsTo(target: User::class)]\n    private User $user;\n}\n```\n\n\u003e **Note**\n\u003e Read more about [BelongsTo](https://cycle-orm.dev/docs/relation-belongs-to).\n\n#### RefersTo\n\n```php\nuse Cycle\\Annotated\\Annotation\\Entity;\nuse Cycle\\Annotated\\Annotation\\Relation\\RefersTo;\nuse Cycle\\Annotated\\Annotation\\Relation\\HasMany;\n\n#[Entity]\nclass User\n{\n    // ...\n\n    #[RefersTo(target: Comment::class)]\n    private ?Comment $lastComment;\n\n    #[HasMany(target: Comment::class)]\n    public array $comments;\n\n    // ...\n\n    public function addComment(Comment $c): void\n    {\n        $this-\u003elastComment = $c;\n        $this-\u003ecomments[] = $c;\n    }\n    \n    public function removeLastComment(): void\n    {\n        $this-\u003elastComment = null;\n    }\n    \n    public function getLastComment(): ?Comment\n    {\n        return $this-\u003elastComment;\n    }\n}\n```\n\n\u003e **Note**\n\u003e Read more about [RefersTo](https://cycle-orm.dev/docs/relation-refers-to).\n\n#### ManyToMany\n\n```php\nuse Cycle\\Annotated\\Annotation\\Relation\\ManyToMany;\nuse Cycle\\Annotated\\Annotation\\Entity;\n\n#[Entity]\nclass User\n{\n    // ...\n\n    #[ManyToMany(target: Tag::class, through: UserTag::class)]\n    protected array $tags;\n    \n    public function getTags(): array\n    {\n        return $this-\u003etags;\n    }\n    \n    public function addTag(Tag $tag): void\n    {\n        $this-\u003etags[] = $tag;\n    }\n    \n    public function removeTag(Tag $tag): void\n    {\n        $this-\u003etags = array_filter($this-\u003etags, static fn(Tag $t) =\u003e $t !== $tag);\n    }\n}\n```\n\n\u003e **Note**\n\u003e Read more about [ManyToMany](https://cycle-orm.dev/docs/relation-many-to-many).\n\n#### Embedded Entities\n\n```php\nuse Cycle\\Annotated\\Annotation\\Embeddable;\nuse Cycle\\Annotated\\Annotation\\Column;\nuse Cycle\\Annotated\\Annotation\\Entity;\nuse Cycle\\Annotated\\Annotation\\Relation\\Embedded;\n\n#[Embeddable]\nclass UserCredentials\n{\n    #[Column(type: 'string(255)')]\n    public string $username;\n\n    #[Column(type: 'string')]\n    public string $password;\n}\n\n#[Entity]\nclass User\n{\n    #[Column(type: 'primary')]\n    public int $id;\n\n    #[Embedded(target: 'UserCredentials')]\n    public UserCredentials $credentials;\n\n    public function __construct()\n    {\n        $this-\u003ecredentials = new UserCredentials();\n    }\n}\n```\n\n\u003e **Note**\n\u003e Read more about [Embedded Entities](https://cycle-orm.dev/docs/relation-embedded).\n\n#### BelongsToMorphed\n\n```php\nuse Cycle\\Annotated\\Annotation\\Entity;\nuse Cycle\\Annotated\\Annotation\\Relation\\Morphed\\BelongsToMorphed;\n\n#[Entity]\nclass Image\n{\n    // ...\n\n    #[BelongsToMorphed(taget: ImageHolderInterface::class)]\n    public ImageHolderInterface $imageHolder;\n}\n```\n\n\u003e **Note**\n\u003e Read more about [BelongsToMorphed](https://cycle-orm.dev/docs/relation-morphed#belongstomorphed).\n\n#### MorphedHasOne\n\n```php\nuse Cycle\\Annotated\\Annotation\\Entity;\nuse Cycle\\Annotated\\Annotation\\Relation\\Morphed\\MorphedHasOne;\n\n#[Entity]\nclass User\n{\n    // ...\n\n    #[MorphedHasOne(target: Image::class)]\n    public $image;\n}\n```\n\n\u003e **Note**\n\u003e Read more about [MorphedHasOne](https://cycle-orm.dev/docs/relation-morphed#morphedhasone).\n\n#### MorphedHasMany\n\n```php\nuse Cycle\\Annotated\\Annotation\\Entity;\nuse Cycle\\Annotated\\Annotation\\Relation\\Morphed\\MorphedHasMany;\n\n#[Entity]\nclass User\n{\n    // ...\n\n    #[MorphedHasMany(target: Image::class)]\n    public $images;\n}\n```\n\n\u003e **Note**\n\u003e Read more about [MorphedHasMany](https://cycle-orm.dev/docs/relation-morphed#morphedhasmany).\n\n### Single Table Inheritance\n\n```php\n#[Entity]\n#[DiscriminatorColumn(name: 'type')] // Discriminator column (required)\nclass Person\n{\n    #[Column(type: 'primary', primary: true)]\n    protected int $id;\n\n    #[Column(type: 'string')]\n    protected string $name;\n}\n\n#[Entity]\n#[InheritanceSingleTable]\nclass Employee extends Person\n{\n    #[Column(type: 'int')]\n    protected int $salary;\n}\n\n#[Entity]\n#[InheritanceSingleTable(value: 'foo_customer')]\nclass Customer extends Person\n{\n    #[Column(type: 'json')]\n    protected array $preferences;\n}\n```\n\n\u003e **Note**\n\u003e Read more about [Single Table Inheritance](https://cycle-orm.dev/docs/advanced-single-table-inheritance).\n\n### Joined Table Inheritance\n\n```php\n#[Entity]\nclass Person\n{\n    #[Column(primary: true)]\n    protected int $id;\n    \n    #[Column()]\n    protected int $fooId;\n\n    #[Column(type: 'string')]\n    protected string $name;\n}\n\n#[Entity]\n#[InheritanceJoinedTable(outerKey: 'fooId')]\nclass Employee extends Person\n{\n    #[Column(type: 'int')]\n    protected int $salary;\n}\n\n#[Entity]\n#[InheritanceJoinedTable(outerKey: 'id')]\nclass Customer extends Person\n{\n    #[Column(type: 'json')]\n    protected array $preferences;\n}\n```\n\n\u003e **Note**\n\u003e Read more about [Joined Table Inheritance](https://cycle-orm.dev/docs/advanced-joined-table-inheritance).\n\n## License\n\nThe MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information. Maintained\nby [Spiral Scout](https://spiralscout.com).\n","funding_links":["https://github.com/sponsors/cycle"],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcycle%2Fannotated","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcycle%2Fannotated","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcycle%2Fannotated/lists"}