Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cycle/annotated
Schema generation using annotated entities and mappers
https://github.com/cycle/annotated
attributes cycle datamapper hacktoberfest orm php
Last synced: 5 days ago
JSON representation
Schema generation using annotated entities and mappers
- Host: GitHub
- URL: https://github.com/cycle/annotated
- Owner: cycle
- License: mit
- Created: 2019-03-26T12:01:44.000Z (over 5 years ago)
- Default Branch: 4.x
- Last Pushed: 2024-09-03T15:05:14.000Z (2 months ago)
- Last Synced: 2024-10-03T22:14:55.312Z (about 1 month ago)
- Topics: attributes, cycle, datamapper, hacktoberfest, orm, php
- Language: PHP
- Homepage:
- Size: 517 KB
- Stars: 24
- Watchers: 4
- Forks: 13
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Cycle ORM - Annotated Entities
[![PHP Version Require](https://poser.pugx.org/cycle/annotated/require/php)](https://packagist.org/packages/cycle/annotated)
[![Latest Stable Version](https://poser.pugx.org/cycle/annotated/v/stable)](https://packagist.org/packages/cycle/annotated)
[![phpunit](https://github.com/cycle/annotated/actions/workflows/main.yml/badge.svg)](https://github.com/cycle/annotated/actions)
[![psalm](https://github.com/cycle/annotated/actions/workflows/psalm.yml/badge.svg)](https://github.com/cycle/annotated/actions)
[![psalm-level](https://shepherd.dev/github/cycle/annotated/level.svg)](https://shepherd.dev/github/cycle/annotated)
[![Codecov](https://codecov.io/gh/cycle/annotated/branch/4.x/graph/badge.svg)](https://codecov.io/gh/cycle/annotated/)
[![Total Downloads](https://poser.pugx.org/cycle/annotated/downloads)](https://packagist.org/packages/cycle/annotated)[Documentation](https://cycle-orm.dev/docs/annotated-prerequisites) | [Cycle ORM](https://github.com/cycle/orm)
The package provides the ability to define Cycle ORM schema using PHP attributes.
## Usage
```php
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
```php
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](https://cycle-orm.dev/docs/relation-has-one).#### HasMany
```php
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](https://cycle-orm.dev/docs/relation-has-many).#### BelongsTo
```php
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](https://cycle-orm.dev/docs/relation-belongs-to).#### RefersTo
```php
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](https://cycle-orm.dev/docs/relation-refers-to).#### ManyToMany
```php
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](https://cycle-orm.dev/docs/relation-many-to-many).#### Embedded Entities
```php
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](https://cycle-orm.dev/docs/relation-embedded).#### BelongsToMorphed
```php
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](https://cycle-orm.dev/docs/relation-morphed#belongstomorphed).#### MorphedHasOne
```php
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](https://cycle-orm.dev/docs/relation-morphed#morphedhasone).#### MorphedHasMany
```php
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](https://cycle-orm.dev/docs/relation-morphed#morphedhasmany).### Single Table Inheritance
```php
#[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](https://cycle-orm.dev/docs/advanced-single-table-inheritance).### Joined Table Inheritance
```php
#[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](https://cycle-orm.dev/docs/advanced-joined-table-inheritance).## License
The MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information. Maintained
by [Spiral Scout](https://spiralscout.com).