July 5th, 2014 · php zend framework 2 doctrine orm

DoctrineModule CLI and multiple entity managers

In working on a project I came across this issue with DoctrineModule - it's Doctrine CLI doesn't support multiple entity managers. In my project each module uses it's own sandboxed Doctrine config, and this works fine when testing or actually using the application. However, when deploying the application to a staging server bin/doctrine orm:schema-tool:create complained about having no managed entities; it was looking in orm_default and not finding anything.

Long and short of it is if you're also having this issue, here's a quick hack that will allow you to work around it:

https://gist.github.com/adamlundrigan/c0c87fc5657453212cd0

Essentially, i've just taken the stock doctrine-module.php file from DoctrineModule and added this immediately after the CLI application is initialized:

// 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');
}

Which creates a new EntityManagerHelper and stores it as em in the CLI helper set.