Skip to content

Instantly share code, notes, and snippets.

@ludekstepan
Created September 20, 2012 11:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ludekstepan/3755289 to your computer and use it in GitHub Desktop.
Save ludekstepan/3755289 to your computer and use it in GitHub Desktop.
Example of container aware migration
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration,
Doctrine\DBAL\Schema\Schema;
class Version20120920130854 extends AbstractMigration
{
public function up(Schema $schema)
{
// ...
$container = $this->version->getConfiguration()->getContainer();
$em = $container->get('doctrine.orm.entity_manager');
// ... use EntityManager and other services
}
public function down(Schema $schema)
{
// ...
}
}
@hafichuk
Copy link

I'm getting an exception trying to do this.

PHP Fatal error:  Call to undefined method Doctrine\DBAL\Migrations\Configuration\YamlConfiguration::getContainer() in /home/bhafichuk/workspace/icarus/db/migrations/Version20121029114408.php on line 20
PHP Stack trace:
PHP   1. {main}() /home/bhafichuk/workspace/icarus/scripts/doctrine:0
PHP   2. Symfony\Component\Console\Application->run() /home/bhafichuk/workspace/icarus/scripts/doctrine:172
PHP   3. Symfony\Component\Console\Application->doRun() /home/bhafichuk/workspace/icarus/source/library/Symfony/Component/Console/Application.php:106
PHP   4. Symfony\Component\Console\Command\Command->run() /home/bhafichuk/workspace/icarus/source/library/Symfony/Component/Console/Application.php:193
PHP   5. Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand->execute() /home/bhafichuk/workspace/icarus/source/library/Symfony/Component/Console/Command/Command.php:239
PHP   6. Doctrine\DBAL\Migrations\Migration->migrate() /home/bhafichuk/workspace/icarus/source/library/Doctrine/DBAL/Migrations/Tools/Console/Command/MigrateCommand.php:115
PHP   7. Doctrine\DBAL\Migrations\Version->execute() /home/bhafichuk/workspace/icarus/source/library/Doctrine/DBAL/Migrations/Migration.php:148
PHP   8. DoctrineMigrations\Version20121029114408->up() /home/bhafichuk/workspace/icarus/source/library/Doctrine/DBAL/Migrations/Version.php:253

@chok
Copy link

chok commented Mar 25, 2013

I had the same issue because of using sf 2.0 and not integrated in 2.0.x-dev of composer package.

You can apply the pull request in your own fork/version to have this working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment