Thanks to visit codestin.com
Credit goes to GitHub.com

Skip to content
forked from cycle/annotated

Schema generation using annotated entities and mappers

License

Notifications You must be signed in to change notification settings

roquie/annotated

 
 

Cycle ORM - Annotated Entities

Latest Stable Version Build Status Scrutinizer Code Quality Codecov

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.

About

Schema generation using annotated entities and mappers

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.7%
  • Shell 0.3%