Skip to content

About

Declarative schema generation for Cycle ORM

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

15 stars

Watchers

6 watching

Forks

Repository files navigation

Cycle ORM Schema Builder

Documentation Sponsor Discord

Psalm Level Type Coverage Codecov Mutation testing badge


Schema Builder configures the Cycle ORM and database schema from entity definitions, produced by annotations (attributes) or by custom generators.

Get Started

Installation

composer require cycle/schema-builder

PHP Latest Version on Packagist License Total Downloads

Requires PHP 8.2+.

Configuration

use Cycle\Database;
use Cycle\Database\Config;

$dbal = new Database\DatabaseManager(new Config\DatabaseConfig([
    'default' => 'default',
    'databases' => [
        'default' => [
            'connection' => 'sqlite'
        ]
    ],
    'connections' => [
        'sqlite' => new Config\SQLiteDriverConfig(
            connection: new Config\SQLite\MemoryConnectionConfig(),
            queryCache: true,
        ),
    ]
]));

$registry = new \Cycle\Schema\Registry($dbal);

Registering entities

Register an entity, add its columns and link it to a table:

use Cycle\Schema\Definition;

$entity = new Definition\Entity();

$entity
    ->setRole('user')
    ->setClass(User::class);

// add fields
$entity->getFields()
    ->set('id', (new Definition\Field())->setType('primary')->setColumn('id')->setPrimary(true))
    ->set('name', (new Definition\Field())->setType('string(32)')->setColumn('user_name'));

// register entity
$registry->register($entity);

// associate table
$registry->linkTable($entity, 'default', 'users');

Compiling the schema

Generate the ORM schema with Cycle\Schema\Compiler:

use Cycle\Schema\Compiler;

$schema = (new Compiler())->compile($registry);

$orm = $orm->with(schema: new \Cycle\ORM\Schema($schema));

More about the Schema Builder is in the documentation.

Testing

The tests run on Testo through the shared cycle/testing harness: tests/Unit needs no database, and tests/Feature runs once per database driver.

composer test:unit         # no database
composer test:sqlite       # in-memory SQLite, no docker
composer db:up             # MySQL, Postgres and SQL Server in docker
composer test              # every suite; a driver whose server is down is skipped
composer db:down

composer test:mysql, test:postgres and test:sqlserver run one driver.

About

Declarative schema generation for Cycle ORM

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

15 stars

Watchers

6 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages