Skip to content

Commit

Permalink
Allow enabling of auto_mapping when multiple entity managers are defined
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-leavitt-sonyatv-com committed Feb 7, 2014
1 parent 765b0d8 commit c2c5559
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ protected function ormLoad(array $config, ContainerBuilder $container)
}

$container->setAlias('doctrine.orm.entity_manager', sprintf('doctrine.orm.%s_entity_manager', $config['default_entity_manager']));

$config['entity_managers'] = $this->collectAutoMappings($config['entity_managers'], $container);

foreach ($config['entity_managers'] as $name => $entityManager) {
$entityManager['name'] = $name;
Expand All @@ -266,6 +268,43 @@ protected function ormLoad(array $config, ContainerBuilder $container)
}
}

/**
* @param array $entityManagerConfigs An array of entity manager configurations
* @param ContainerBuilder $container A ContainerBuilder instance
*
* @throws \InvalidArgumentException
*
* @return array
*/
protected function collectAutoMappings(array $entityManagerConfigs, ContainerBuilder $container)
{
$definedBundles = array_keys($container->getParameter('kernel.bundles'));

foreach ($entityManagerConfigs as $name => &$entityManager) {
if (isset($entityManager['auto_mapping']) && $entityManager['auto_mapping']) {
foreach ($definedBundles as $bundle) {
foreach ($entityManagerConfigs as $name2 => $entityManager2) {
if ($name2 !== $name) {
if (isset($entityManager2['auto_mapping']) && $entityManager2['auto_mapping']) {
throw new \LogicException(sprintf('You cannot enable "auto_mapping" on more than one entity manager at the same time (found in "%s" and %s").', $name, $name2));
}

if (isset($entityManager2['mappings'][$bundle])) {
continue 2;
}
}
}

$entityManager['mappings'][$bundle]['mapping'] = true;
}

$entityManager['auto_mapping'] = false;
}
}

return $entityManagerConfigs;
}

/**
* Loads a configured ORM entity manager.
*
Expand All @@ -274,10 +313,6 @@ protected function ormLoad(array $config, ContainerBuilder $container)
*/
protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $container)
{
if ($entityManager['auto_mapping'] && count($this->entityManagers) > 1) {
throw new \LogicException('You cannot enable "auto_mapping" when several entity managers are defined.');
}

$ormConfigDef = $container->setDefinition(sprintf('doctrine.orm.%s_configuration', $entityManager['name']), new DefinitionDecorator('doctrine.orm.configuration'));

$this->loadOrmEntityManagerMappingInformation($entityManager, $ormConfigDef, $container);
Expand Down

0 comments on commit c2c5559

Please sign in to comment.