2017 © Pedro Peláez
 

library dbal-schema

DB schema manager for Doctrine DBAL

image

mnapoli/dbal-schema

DB schema manager for Doctrine DBAL

  • Saturday, December 23, 2017
  • by mnapoli
  • Repository
  • 3 Watchers
  • 56 Stars
  • 338 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

A schema manager for Doctrine DBAL: all the convenience of the Doctrine ORM, without using the ORM., (*1)

Why?

Doctrine ORM can automatically manage your DB schema based on your entity mapping. This feature is lost when using the DBAL instead of the ORM., (*2)

This package lets you achieve something similar by defining your DB schema with PHP code. It also lets you manage your database using a Symfony Console command similar to Symfony's native doctrine:schema:update command, as well as DB migrations., (*3)

Installation

composer require mnapoli/dbal-schema

Usage

1. Define a schema

Define your DB schema by implementing the SchemaDefinition interface:, (*4)

class MySchemaDefinition implements \DbalSchema\SchemaDefinition
{
    public function define(Schema $schema)
    {
        $usersTable = $schema->createTable('users');
        $usersTable->addColumn('id', 'integer');
        $usersTable->addColumn('email', 'string');
        $usersTable->addColumn('lastLogin', 'datetime');
        $usersTable->addColumn('score', 'float', [
            'notnull' => false,
        ]);
        $usersTable->setPrimaryKey(['id']);
        $usersTable->addUniqueIndex(['email']);
    }
}

You can read the whole API available on Doctrine's documentation., (*5)

2. Set up the schema

Doctrine can now generate/update your database based on your schema., (*6)

Using Symfony

Here is an example of configuration that can go in your config/services.yml:, (*7)

services:

    DbalSchema\SchemaDefinition:
        # Replace this with your class name
        alias: App\Database\MySchemaDefinition
    DbalSchema\DbalSchemaProvider:
    # Register the commands:
    DbalSchema\DbalSchemaCommand:
    DbalSchema\Command\UpdateCommand:
    DbalSchema\Command\PurgeCommand:

This configuration assumes your services are autowired., (*8)

Once the services are registered, you can now run the commands:, (*9)

bin/console dbal:schema:update
bin/console dbal:schema:purge

Using Silly

Using Silly you can ignore the many separate command classes and simply use the DbalSchemaCommand class:, (*10)

$schema = new MySchemaDefinition();
$dbalConnection = /* your DBAL connection, see http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html */

$command = new DbalSchemaCommand($dbalConnection, $schema);

$application = new Silly\Application();
$application->command('db [--force]', [$command, 'update']);
$application->command('db-purge [--force]', [$command, 'purge']);
$application->run();

If you are using the Silly PHP-DI edition it's even simpler as PHP-DI can instantiate the DbalSchemaCommand service:, (*11)

$application->command('db [--force]', [DbalSchemaCommand::class, 'update']);
$application->command('db-purge [--force]', [DbalSchemaCommand::class, 'purge']);

3. Optional: database migrations

If you prefer using database migrations instead of running bin/console dbal:schema:update, DBAL Schema integrates with Doctrine Migrations., (*12)

To set it up, we need the setService() method call to happen like in the example below:, (*13)

use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Provider\SchemaProvider;
use DbalSchema\DbalSchemaProvider;

$doctrineMigrationDependencyFactory = DependencyFactory::fromConnection(...);

$doctrineMigrationDependencyFactory->setService(SchemaProvider::class, new DbalSchemaProvider(new MySchemaDefinition()));

In Symfony, it can be done by installing the DoctrineMigrationsBundle and editing config/packages/doctrine_migrations.yaml:, (*14)

doctrine_migrations:
    # ...
    services:
        Doctrine\Migrations\Provider\SchemaProvider: DbalSchema\DbalSchemaProvider

Now, you can run:, (*15)

bin/console doctrine:migrations:diff

to generate migrations based on your DBAL schema., (*16)

The Versions

23/12 2017

dev-master

9999999-dev

DB schema manager for Doctrine DBAL

  Sources   Download

MIT

The Requires

 

The Development Requires

schema doctrine dbal db

23/12 2017

1.0.0

1.0.0.0

DB schema manager for Doctrine DBAL

  Sources   Download

MIT

The Requires

 

The Development Requires

schema doctrine dbal db

27/03 2017

0.2.1

0.2.1.0

DB schema manager for Doctrine DBAL

  Sources   Download

MIT

The Requires

 

The Development Requires

schema doctrine dbal db

25/03 2017

0.2.0

0.2.0.0

DB schema manager for Doctrine DBAL

  Sources   Download

MIT

The Requires

 

The Development Requires

schema doctrine dbal db

24/03 2017

0.1.0

0.1.0.0

Schema manager for Doctrine DBAL

  Sources   Download

MIT

The Requires

 

schema doctrine dbal