Skip to content

Repository files navigation

Cycle ORM - Annotated Entities

PHP Version RequireLatest Stable Versionphpunitpsalmpsalm-levelCodecovTotal Downloads

Documentation | Cycle ORM

The package provides the ability to define Cycle ORM schema using PHP attributes.

Usage

useCycle\Annotated\Annotation\Entity;
useCycle\Annotated\Annotation\Column;
#[Entity]
class User
{
#[Column(type: 'primary')]
privateint$id;
#[Column(type: 'string(32)')]
privatestring$login;
#[Column(type: 'enum(active,disabled)')]
privatestring$status;
#[Column(type: 'decimal(5,5)')]
private$balance;
}

Relations

HasOne

useCycle\Annotated\Annotation\Relation\HasOne;
useCycle\Annotated\Annotation\Entity;
#[Entity]
class User
{
// ...
#[HasOne(target: Address::class)]
public ?Address$address;
}

Note Read more about HasOne.

HasMany

useCycle\Annotated\Annotation\Entity;
useCycle\Annotated\Annotation\Relation\HasMany;
#[Entity]
class User
{
// ...
#[HasMany(target: Post::class)]
privatearray$posts;
}

Note Read more about HasMany.

BelongsTo

useCycle\Annotated\Annotation\Entity;
useCycle\Annotated\Annotation\Relation\BelongsTo;
#[Entity]
class Post
{
// ...
#[BelongsTo(target: User::class)]
privateUser$user;
}

Note Read more about BelongsTo.

RefersTo

useCycle\Annotated\Annotation\Entity;
useCycle\Annotated\Annotation\Relation\RefersTo;
useCycle\Annotated\Annotation\Relation\HasMany;
#[Entity]
class User
{
// ...
#[RefersTo(target: Comment::class)]
private ?Comment$lastComment;
#[HasMany(target: Comment::class)]
publicarray$comments;
// ...publicfunctionaddComment(Comment$c): void
{
$this->lastComment = $c;
$this->comments[] = $c;
}
publicfunctionremoveLastComment(): void
{
$this->lastComment = null;
}
publicfunctiongetLastComment(): ?Comment
{
return$this->lastComment;
}
}

Note Read more about RefersTo.

ManyToMany

useCycle\Annotated\Annotation\Relation\ManyToMany;
useCycle\Annotated\Annotation\Entity;
#[Entity]
class User
{
// ...
#[ManyToMany(target: Tag::class, through: UserTag::class)]
protectedarray$tags;
publicfunctiongetTags(): array
{
return$this->tags;
}
publicfunctionaddTag(Tag$tag): void
{
$this->tags[] = $tag;
}
publicfunctionremoveTag(Tag$tag): void
{
$this->tags = array_filter($this->tags, staticfn(Tag$t) => $t !== $tag);
}
}

Note Read more about ManyToMany.

Embedded Entities

useCycle\Annotated\Annotation\Embeddable;
useCycle\Annotated\Annotation\Column;
useCycle\Annotated\Annotation\Entity;
useCycle\Annotated\Annotation\Relation\Embedded;
#[Embeddable]
class UserCredentials
{
#[Column(type: 'string(255)')]
publicstring$username;
#[Column(type: 'string')]
publicstring$password;
}
#[Entity]
class User
{
#[Column(type: 'primary')]
publicint$id;
#[Embedded(target: 'UserCredentials')]
publicUserCredentials$credentials;
publicfunction__construct()
{
$this->credentials = newUserCredentials();
}
}

Note Read more about Embedded Entities.

BelongsToMorphed

useCycle\Annotated\Annotation\Entity;
useCycle\Annotated\Annotation\Relation\Morphed\BelongsToMorphed;
#[Entity]
class Image
{
// ...
#[BelongsToMorphed(taget: ImageHolderInterface::class)]
publicImageHolderInterface$imageHolder;
}

Note Read more about BelongsToMorphed.

MorphedHasOne

useCycle\Annotated\Annotation\Entity;
useCycle\Annotated\Annotation\Relation\Morphed\MorphedHasOne;
#[Entity]
class User
{
// ...
#[MorphedHasOne(target: Image::class)]
public$image;
}

Note Read more about MorphedHasOne.

MorphedHasMany

useCycle\Annotated\Annotation\Entity;
useCycle\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)]
protectedint$id;
#[Column(type: 'string')]
protectedstring$name;
}
#[Entity]
#[InheritanceSingleTable]
class Employee extends Person
{
#[Column(type: 'int')]
protectedint$salary;
}
#[Entity]
#[InheritanceSingleTable(value: 'foo_customer')]
class Customer extends Person
{
#[Column(type: 'json')]
protectedarray$preferences;
}

Note Read more about Single Table Inheritance.

Joined Table Inheritance

#[Entity]
class Person
{
#[Column(primary: true)]
protectedint$id;
#[Column()]
protectedint$fooId;
#[Column(type: 'string')]
protectedstring$name;
}
#[Entity]
#[InheritanceJoinedTable(outerKey: 'fooId')]
class Employee extends Person
{
#[Column(type: 'int')]
protectedint$salary;
}
#[Entity]
#[InheritanceJoinedTable(outerKey: 'id')]
class Customer extends Person
{
#[Column(type: 'json')]
protectedarray$preferences;
}

Note Read more about Joined Table Inheritance.

License

The MIT License (MIT). Please see LICENSE for more information. Maintained by Spiral Scout.

About

Schema generation using annotated entities and mappers

Topics

Resources

Code of conduct

Contributing

Stars

29 stars

Watchers

3 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages