Skip to content

Instantly share code, notes, and snippets.

@benr77
Created August 13, 2022 15:32
Show Gist options
  • Save benr77/ccd83565dc47197aa24624da8da4f4d6 to your computer and use it in GitHub Desktop.
Save benr77/ccd83565dc47197aa24624da8da4f4d6 to your computer and use it in GitHub Desktop.
Auto-registering Doctrine mappings in a Symfony bundle
<?php
declare(strict_types=1);
namespace Acme\DemoBundle;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeDemoBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$mappingFileLocation = '/Doctrine/Mapping';
$modelDir = realpath(__DIR__ . $mappingFileLocation);
$mappings = [
$modelDir => 'Demo',
];
if (class_exists(DoctrineOrmMappingsPass::class)) {
$container->addCompilerPass(
DoctrineOrmMappingsPass::createXmlMappingDriver(
$mappings,
['doctrine.orm.entity_manager'],
false
)
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment