Skip to content

Instantly share code, notes, and snippets.

@adamlundrigan
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamlundrigan/c0c87fc5657453212cd0 to your computer and use it in GitHub Desktop.
Save adamlundrigan/c0c87fc5657453212cd0 to your computer and use it in GitHub Desktop.
Multiple-EM-aware DoctrineModule CLI

Installation

  • Place the doctrine script below in your application's bin directory
  • Make it executable (or prepend php to the command below)

Usage

EM_ALIAS="my-custom-em" bin/doctrine 

The CLI commands will now run using the my-custom-em Entity Manager.

#!/usr/bin/env php
<?php
use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\Application;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
ini_set('display_errors', true);
chdir(__DIR__);
$previousDir = '.';
while (!file_exists('config/application.config.php')) {
$dir = dirname(getcwd());
if ($previousDir === $dir) {
throw new RuntimeException(
'Unable to locate "config/application.config.php": ' .
'is DoctrineModule in a subdir of your application skeleton?'
);
}
$previousDir = $dir;
chdir($dir);
}
if (is_readable('init_autoloader.php')) {
include_once 'init_autoloader.php';
} elseif (!(@include_once __DIR__ . '/../vendor/autoload.php') && !(@include_once __DIR__ . '/../../../autoload.php')) {
throw new RuntimeException('Error: vendor/autoload.php could not be found. Did you run php composer.phar install?');
}
$application = Application::init(include 'config/application.config.php');
/* @var $cli \Symfony\Component\Console\Application */
$cli = $application->getServiceManager()->get('doctrine.cli');
// If we've overridden the entity manager via env var, inject the new one
$entityManagerName = getenv('EM_ALIAS') ?: 'orm_default';
if ( $application->getServiceManager()->has($entityManagerName) ) {
$emHelper = new EntityManagerHelper($application->getServiceManager()->get($entityManagerName));
$cli->getHelperSet()->set($emHelper, 'em');
}
$cli->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment