codito/doctrine-migrations-service-provider

此软件包已被弃用,不再维护。未建议替代包。

Doctrine Migrations 服务提供程序,适用于 Silex 应用程序

v2.1.0 2017-05-05 10:47 UTC

This package is auto-updated.

Last update: 2023-10-03 15:23:46 UTC


README

通过扩展控制台以添加额外命令,为 Doctrine Migrations 命令提供 Silex 应用程序的支持。这些命令是基础 Doctrine Migrations 命令的包装器,为了正确工作,控制台应用程序需要是 Codito Console Service Provider 提供的控制台实例,因为命令需要访问一些服务和/或配置选项以正确解析迁移配置。

要求

  • PHP >= 5.4,因为需要 traits 和短数组语法。

安装

composer.json 中添加条目

"require": {
    "codito/doctrine-migrations-service-provider": "~2.0"
}

配置

为了在您的控制台中使用 Doctrine Migrations 命令,您需要配置一些内容

  • DoctrineServiceProvider(Silex 的默认提供程序之一)
  • 这里 获取 ConsoleServiceProvider
  • 这里 获取 DoctrineOrmServiceProvider,可选的(仅对 migrations:diff 命令要求)
  • DoctrineMigrationsServiceProvider 本身

DoctrineMigrationsServiceProvider 支持单连接和多个连接/实体管理器的配置。

示例配置

注册 DoctrineServiceProvider(也可以使用 db.options 进行配置,然后它将是 default 连接)

$app->register(new DoctrineServiceProvider(), array(
    'dbs.options' => array(
        'some_connection' => array(
            'driver'   => 'pdo_mysql',
            'dbname'   => 'silex',
            'host'     => 'localhost',
            'user'     => 'root',
            'password' => null,
            'port'     => null,
        )
    )
));

注册 ConsoleServiceProvider

$app->register(new \Codito\Silex\Provider\ConsoleServiceProvider(), array(
    'console.name'              => 'Silex App',
    'console.version'           => '1.0.0',
));

注册 DoctrineMigrationsServiceProvider

$app->register(new \Codito\Silex\DoctrineMigrationsService\Provider\DoctrineMigrationsServiceProvider(), array(
    'db.migrations.options' => array(
        'some_connection' => array(
            'dir_name' => realpath(__DIR__ . '/Application/Migrations'),
            'namespace' => 'Application\\Migrations',
            'table_name' => 'migration_versions',
            'name' => 'Application Migrations',
        )
    )
));

DoctrineMigrationsServiceProvider 的配置始终位于 db.migrations.options 下,无论是否为单或多个配置。这些配置与 db.options/dbs.options 相关,并且名称必须匹配才能正确工作。

可选地,如果您需要 migrations:diff 命令,您可能需要注册

$app->register(new Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider(), array(
    'orm.proxies_dir' => __DIR__ . '/../var/orm',
    'orm.ems.options' => array(
        'some_entity_manager' => array(
            'connection' => 'some_connection', // Important if you have custom connection name
            'mappings' => array(
                // Using actual filesystem paths
                array(
                    'type' => 'annotation',
                    'namespace' => 'Application\Entity',
                    'path' => __DIR__ . '/Application/Entity',
                    'use_simple_annotation_reader' => false // Support for "use Doctrine\ORM\Mapping AS ORM" -> "@ORM\Entity"
                ),
            ),
        )
    ),
));

如果您想使用复杂的注解,如 @ORM\Entity,则必须将 use_simple_annotation_reader 设置为 false,如上所示。但是,它需要您自己配置 AnnotationRegistry,如下所示

$loader = require_once __DIR__.'/../vendor/autoload.php';
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

应在 console 可执行文件中完成此操作。

用法

只需运行 php bin/console(或您拥有的 console 位置)即可,然后会出现命令列表。每个命令都有自己的帮助,要查看它,只需输入 php bin/console some:command:from:your:commands:list --help

如果您正确完成了所有操作,完整的迁移命令将如下所示

doctrine
  doctrine:migrations:diff       Generate a migration by comparing your current database to your mapping information.
  doctrine:migrations:execute    Execute a single migration version up or down manually.
  doctrine:migrations:generate   Generate a blank migration class.
  doctrine:migrations:latest     Outputs the latest version number
  doctrine:migrations:migrate    Execute a migration to a specified version or the latest available version.
  doctrine:migrations:status     View the status of a set of migrations.
  doctrine:migrations:version    Manually add and delete migration versions from the version table.

容器感知迁移

v2.1 版本开始,可以使用自动注入的 Pimple 容器(Silex 应用程序)使用迁移。如果您需要在迁移中获取容器,只需实现 Codito\Silex\DoctrineMigrationsService\Migration\ContainerAwareMigration 接口或扩展 Codito\Silex\DoctrineMigrationsService\Migration\AbstractContainerAwareMigration。如果您有多个连接,并且一个连接的迁移需要从另一个连接获取数据,或者需要调用某些服务以执行迁移,这将非常有用。

请注意

根据您的配置,您可能需要(或不)传递 --db/--em 参数到命令中。大多数命令使用 --db,而 --em 仅用于 doctrine:migrations:diff,它将根据实体管理器配置中的 connection 属性自动设置 --db 选项。