【问题标题】:Doctrine Migrations with Symfony 3 addIndex not found. Why?未找到使用 Symfony 3 addIndex 的 Doctrine Migrations。为什么?
【发布时间】:2017-12-22 06:19:18
【问题描述】:

我继承了一个没有迁移的 Symfony 3 项目。

documentation 之后,我将 Doctrine Migrations Bundle 添加到 Symfony。

$ composer require doctrine/doctrine-migrations-bundle "^1.0"

将配置添加到 config.yml

doctrine_migrations:
    dir_name: "%kernel.root_dir%/DoctrineMigrations"
    namespace: Application\Migrations
    table_name: migration_versions
    name: Application Migrations

然后将加载器添加到appKernel.php

new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),

控制台现在将教义:迁移:* 显示为可用功能,因此我随后生成了我的迁移文件

php bin/console doctrine:migrations:generate 
Generated new migration class to "/path/to/project/app/DoctrineMigrations/Version20100621140655.php"

然后继续关注Doctrine migrations documentation

public function up(Schema $schema)
{
    // alter my table
    $options = ['fields' => ['type_id']];
    $schema->addIndex('advertiser', 'idx_type_id', $options);
    ...
}

现在,当我运行迁移时:

Migrating up to 20170714165306 from 20170714155549

++ migrating 20170714165306

[Symfony\Component\Debug\Exception\UndefinedMethodException]                                                                                                                
Attempted to call an undefined method named "addIndex" of class "ProxyManagerGeneratedProxy\__PM__\Doctrine\DBAL\Schema\Schema\Generatedaca9b50393335f8354881e485c485329".  

现在,当我遇到该错误时,我搜索并找到了 ocramius/ProxyManager。我用

安装了它
composer require ocramius/proxy-manager "^2.0"

还是同样的错误。

当然,在大多数 Doctrine Migrations 文档中,我主要看到 addSql( 'ALTER TABLE ...') 类型语句,但我想使用包装方法 addIndexremoveIndex 等。

有人能解释一下如何让这些功能发挥作用吗?

【问题讨论】:

  • 引用的文档看起来不像 doctrine-migrations-bundle 包,但是试试这个 $schema->getTable('table_name')->addIndex(...) 看看 addIndex() 签名。

标签: php symfony doctrine-orm database-migration


【解决方案1】:

我发现了我的问题。通过在迁移文件中执行以下操作即可解决该问题:

public function up(Schema $schema)
{
    // alter my table
    $table = $schema->getTable('advertiser');
    $table->addIndex(['type_id'], 'idx_type_id');
    ...
}

基本思路是,你需要抓取一个对表的引用,然后你可以将索引添加到表上的特定列。与 Doctrine 文档相反,您不能在 $schema 引用上添加索引,在 Symfony2/3 迁移中传递表名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2015-01-22
    • 1970-01-01
    • 2015-05-08
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多