mnapoli/dbal-schema

Doctrine DBAL 数据库模式管理器

资助包维护!
mnapoli

1.3.1 2023-12-20 20:41 UTC

This package is auto-updated.

Last update: 2024-09-20 22:14:39 UTC


README

一个用于 Doctrine DBAL 的模式管理器:拥有 Doctrine ORM 的便利性,而不使用 ORM。

为什么?

Doctrine ORM 可以根据您的实体映射自动管理您的数据库模式。当使用 DBAL 而不是 ORM 时,此功能将丢失。

此包允许您通过使用 PHP 代码定义您的数据库模式来实现类似的功能。它还允许您使用类似于 Symfony 的原生 doctrine:schema:update 命令的 Symfony 控制台命令以及数据库迁移来管理您的数据库。

安装

composer require mnapoli/dbal-schema

使用

1. 定义模式

通过实现 SchemaDefinition 接口定义您的数据库模式

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']);
    }
}

您可以在 Doctrine 的文档 中找到整个 API。

2. 设置模式

现在,Doctrine 可以根据您的模式生成/更新数据库。

使用 Symfony

以下是一个可以放入您的 config/services.yml 中的配置示例

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:

此配置假设服务是自动注入的。

一旦服务被注册,您现在可以运行以下命令

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

使用 Silly

使用 Silly,您可以忽略许多单独的命令类,只需使用 DbalSchemaCommand 类即可

$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();

如果您正在使用 Silly PHP-DI 版本,则更加简单,因为 PHP-DI 可以实例化 DbalSchemaCommand 服务

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

3. 可选:数据库迁移

如果您更喜欢使用数据库迁移而不是运行 bin/console dbal:schema:update,则 DBAL Schema 与 Doctrine Migrations 集成。

要设置它,我们需要在下面的示例中调用 setService() 方法

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

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

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

在 Symfony 中,可以通过安装 DoctrineMigrationsBundle 并编辑 config/packages/doctrine_migrations.yaml 来完成此操作

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

现在,您可以通过以下方式运行

bin/console doctrine:migrations:diff

来根据您的 DBAL 模式生成迁移。