Created
March 15, 2014 18:11
-
-
Save pierredup/9571473 to your computer and use it in GitHub Desktop.
Running doctrine migrations from a controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in zf2, configuration is in the service locator, not sure in symfony2
$configuration = $zf2serviceManage->get('doctrine.migrationsconfiguration.ormdefault')