Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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