phpguru
phpguru

Reputation: 2381

Doctrine Migrations with Symfony 3 addIndex not found. Why?

I've inherited a Symfony 3 project that had no migrations.

Following the documentation, I added Doctrine Migrations Bundle to Symfony.

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

Added the configuration to config.yml

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

Then added the loader to appKernel.php

new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),

The console now shows doctrine:migrations:* as available features, so I then generated my migration file

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

Then continued following along with the Doctrine migrations documentation:

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

Now, when I run the migration:

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".  

Now when I got that error, I searched on that and found ocramius/ProxyManager. I installed that with

composer require ocramius/proxy-manager "^2.0"

Still got the same error.

Granted, in most of the Doctrine Migrations documentation, I see mainly addSql( 'ALTER TABLE ...') type statements, but I want to use the wrapper methods addIndex, removeIndex, etc.

Can someone explain how to get these features working?

Upvotes: 0

Views: 1701

Answers (1)

phpguru
phpguru

Reputation: 2381

I figured out my problem. The problem is solved by doing the following in the migration file:

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

The basic idea is that you need to grab a reference to the table, and then you can addIndex to the specific column on the table. Contrary to the Doctrine docs, you can't addIndex on a $schema reference passing the table name within a Symfony2/3 migration.

Upvotes: 1

Related Questions