The package provides the ability to define Cycle ORM schema using PHP attributes.
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;
}useCycle\Annotated\Annotation\Relation\HasOne;
useCycle\Annotated\Annotation\Entity;
#[Entity]
class User
{
// ...
#[HasOne(target: Address::class)]
public ?Address$address;
}Note Read more about HasOne.
useCycle\Annotated\Annotation\Entity;
useCycle\Annotated\Annotation\Relation\HasMany;
#[Entity]
class User
{
// ...
#[HasMany(target: Post::class)]
privatearray$posts;
}Note Read more about HasMany.
useCycle\Annotated\Annotation\Entity;
useCycle\Annotated\Annotation\Relation\BelongsTo;
#[Entity]
class Post
{
// ...
#[BelongsTo(target: User::class)]
privateUser$user;
}Note Read more about BelongsTo.
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.
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.
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.
useCycle\Annotated\Annotation\Entity;
useCycle\Annotated\Annotation\Relation\Morphed\BelongsToMorphed;
#[Entity]
class Image
{
// ...
#[BelongsToMorphed(taget: ImageHolderInterface::class)]
publicImageHolderInterface$imageHolder;
}Note Read more about BelongsToMorphed.
useCycle\Annotated\Annotation\Entity;
useCycle\Annotated\Annotation\Relation\Morphed\MorphedHasOne;
#[Entity]
class User
{
// ...
#[MorphedHasOne(target: Image::class)]
public$image;
}Note Read more about MorphedHasOne.
useCycle\Annotated\Annotation\Entity;
useCycle\Annotated\Annotation\Relation\Morphed\MorphedHasMany;
#[Entity]
class User
{
// ...
#[MorphedHasMany(target: Image::class)]
public$images;
}Note Read more about MorphedHasMany.
#[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.
#[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.
The MIT License (MIT). Please see LICENSE for more information. Maintained
by Spiral Scout.