DB schema manager for Doctrine DBAL.
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.
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.
composer require mnapoli/dbal-schema
Define your DB schema by implementing the SchemaDefinition
interface:
class MySchemaDefinition implements 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');
$usersTable->setPrimaryKey(['id']);
$usersTable->addUniqueIndex(['email']);
}
}
You can read the whole API available on Doctrine's documentation.
You can now let Doctrine update your database to match the schema you defined. This is done using the DbalSchemaCommand
.
TODO
Using Silly
$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, 'setup']);
$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:
$application->command('db [--force]', [DbalSchemaCommand::class, 'setup']);
$application->command('db-purge [--force]', [DbalSchemaCommand::class, 'purge']);