Skip to content

Instantly share code, notes, and snippets.

@pierredup
Created March 15, 2014 18:11
Show Gist options
  • Save pierredup/9571473 to your computer and use it in GitHub Desktop.
Save pierredup/9571473 to your computer and use it in GitHub Desktop.
Running doctrine migrations from a controller
<?php
namespace Acme\DemiBundle\Controller;
use Doctrine\DBAL\Migrations\Migration;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
class MigrateController extends BaseController
{
public function indexAction()
{
$container = $this->container;
$conn = $this->get('doctrine')->getConnection();
$dir = $container->getParameter('doctrine_migrations.dir_name');
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
$configuration = new Configuration($conn);
$configuration->setMigrationsNamespace($container->getParameter('doctrine_migrations.namespace'));
$configuration->setMigrationsDirectory($dir);
$configuration->registerMigrationsFromDirectory($dir);
$configuration->setName($container->getParameter('doctrine_migrations.name'));
$configuration->setMigrationsTableName($container->getParameter('doctrine_migrations.table_name'));
$versions = $configuration->getMigrations();
foreach ($versions as $version) {
$migration = $version->getMigration();
if ($migration instanceof ContainerAwareInterface) {
$migration->setContainer($container);
}
}
$migration = new Migration($configuration);
$migrated = $migration->migrate();
// ...
}
}
@elvisciotti
Copy link

in zf2, configuration is in the service locator, not sure in symfony2
$configuration = $zf2serviceManage->get('doctrine.migrationsconfiguration.ormdefault')

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