cycle / annotated
Cycle ORM Annotated Entities generator
Fund package maintenance!
roadrunner-server
Installs: 305 632
Dependents: 31
Suggesters: 2
Security: 0
Stars: 21
Watchers: 4
Forks: 11
Open Issues: 6
Requires
- php: >=8.0
- cycle/orm: ^2.2.0
- cycle/schema-builder: ^2.4
- doctrine/annotations: ^1.14.3 || ^2.0.1
- doctrine/inflector: ^2.0
- spiral/attributes: ^2.8|^3.0
- spiral/tokenizer: ^2.8|^3.0
Requires (Dev)
- phpunit/phpunit: ^9.6
- vimeo/psalm: ^4.30
README
Simple example:
Annotation definition
/** * @Entity( * role="user", * repository="Repository/UserRepository", * typecast="Typecast\AutoTypecaster" * ) */ class User { /** @Column(type="primary") */ protected $id; /** @HasOne(target=Profile::class, load="eager") */ protected $profile; /** @HasMany(target=Post::class, load="lazy") */ protected $posts; /** * @ManyToMany( * target=Tag::class, * through=TagMap::class, * load="lazy", * collection="Collection\BaseCollection" * ) */ protected $tags; ... }
Attribute definition
#[Entity( role: "user", repository: Repository/UserRepository::class, typecast: Typecast\Typecaster::class )] class User { #[Column(type: 'primary')] protected $id; #[HasOne(target: Profile::class, load: "eager")] protected $profile; #[HasMany(target: Post::class, load: "lazy")] protected $posts; #[ManyToMany( target: Tag::class, through: TagMap::class, load: "lazy", collection: Collection\BaseCollection::class )] protected $tags; ... }
STI/JTI:
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; }
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; }
Combined example
#[Entity] #[DiscriminatorColumn(name: 'type')] 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; } #[Entity] #[InheritanceJoinedTable(outerKey: 'foo_id')] class Executive extends Employee { #[Column(type: 'int')] protected int $bonus; }
Schema modifiers:
Schema modifier example
namespace App\SchemaModifiers; use Cycle\ORM\SchemaInterface; use Cycle\Schema\Registry; use Cycle\Schema\SchemaModifierInterface; #[\Attribute(\Attribute::TARGET_CLASS)] class MapperSegmentSchemaModifier implements SchemaModifierInterface { private string $role; public function __construct( private string $class ) { } public function withRole(string $role): static { $this->role = $role; return $this; } public function compute(Registry $registry): void { // ... } public function render(Registry $registry): void { // ... } public function modifySchema(array &$schema): void { $schema[SchemaInterface::MAPPER] = $this->class; } }
Usage
namespace App\Entities; use Cycle\Annotated\Annotation\Column; use Cycle\Annotated\Annotation\Entity; use App\SchemaModifiers\MapperSegmentSchemaModifier; #[Entity] #[MapperSegmentSchemaModifier(class: SuperMapper::class)] class Post { #[Column(type: 'integer', primary: true)] protected int $id; #[Column(type: 'string')] protected string $name; }
License:
The MIT License (MIT). Please see LICENSE
for more information. Maintained
by Spiral Scout.