From dbe21d7368b07ad946f7017076f25be886206cce Mon Sep 17 00:00:00 2001 From: Bob van de Vijver Date: Wed, 22 Oct 2025 11:28:43 +0200 Subject: [PATCH 1/4] Fix attribute driver mapping pass compatibility with ORM 3 --- .../Compiler/DoctrineOrmMappingsPass.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php b/src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php index 4ec6bd378..cce03485d 100644 --- a/src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php +++ b/src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php @@ -16,6 +16,8 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; +use function method_exists; + /** * Class for Symfony bundles to configure mappings for model classes not in the * auto-mapped folder. @@ -177,7 +179,12 @@ public static function createAnnotationMappingDriver(array $namespaces, array $d */ public static function createAttributeMappingDriver(array $namespaces, array $directories, array $managerParameters = [], $enabledParameter = false, array $aliasMap = [], bool $reportFieldsWhereDeclared = false) { - $driver = new Definition(AttributeDriver::class, [$directories, $reportFieldsWhereDeclared]); + $driverArgs = [$directories]; + if (method_exists(AttributeDriver::class, 'getReader')) { + $driverArgs[] = $reportFieldsWhereDeclared; + } + + $driver = new Definition(AttributeDriver::class, $driverArgs); return new DoctrineOrmMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap); } From c60a1600479fdd8b4615ccba496b52d44fae7d76 Mon Sep 17 00:00:00 2001 From: Bob van de Vijver Date: Mon, 22 Dec 2025 10:56:28 +0100 Subject: [PATCH 2/4] Add test case to validate attribute driver registration --- .../Compiler/DoctrineOrmMappingsPassTest.php | 26 +++++++++++++++++++ tests/TestCase.php | 7 ++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php b/tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php index 1736cef2a..96b2ea118 100644 --- a/tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php +++ b/tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php @@ -7,7 +7,13 @@ use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass; use Doctrine\Bundle\DoctrineBundle\Tests\TestCase; use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; +use Doctrine\ORM\Mapping\Driver\AttributeDriver; +use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use PHPUnit\Framework\Attributes\IgnoreDeprecations; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +use function assert; +use function realpath; class DoctrineOrmMappingsPassTest extends TestCase { @@ -31,4 +37,24 @@ public function testCreateAnnotationMappingDriverIsDeprecated(): void ['/path/to/entities'], ); } + + public function testAttributeDriverIsRegistered(): void + { + $driverNamespace = 'DoctrineBundle\Entity'; + $container = $this->createXmlBundleTestContainer( + static function (ContainerBuilder $containerBuilder) use ($driverNamespace): void { + $containerBuilder->addCompilerPass(DoctrineOrmMappingsPass::createAttributeMappingDriver( + [$driverNamespace], + [realpath(__DIR__ . '/Entity')], + reportFieldsWhereDeclared: true, + )); + }, + ); + + $metadataDriver = $container->get('doctrine.orm.default_metadata_driver'); + assert($metadataDriver instanceof MappingDriverChain); + + $driver = $metadataDriver->getDrivers()[$driverNamespace]; + $this->assertTrue($driver instanceof AttributeDriver); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 137460c26..962e05730 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -22,7 +22,7 @@ class TestCase extends BaseTestCase { - public function createXmlBundleTestContainer(): ContainerBuilder + public function createXmlBundleTestContainer(callable|null $func = null): ContainerBuilder { $container = new ContainerBuilder(new ParameterBag([ 'kernel.debug' => false, @@ -90,6 +90,11 @@ public function createXmlBundleTestContainer(): ContainerBuilder $compilerPassConfig->addPass(new CacheCompatibilityPass()); // make all Doctrine services public, so we can fetch them in the test $compilerPassConfig->addPass(new TestCaseAllPublicCompilerPass()); + + if ($func !== null) { + $func($container); + } + $container->compile(); return $container; From 099d22cf0ce4bf6df57b09a69239b51c65fe279e Mon Sep 17 00:00:00 2001 From: Bob van de Vijver Date: Mon, 22 Dec 2025 11:02:25 +0100 Subject: [PATCH 3/4] Test requires the ORM to be installed --- .../Compiler/DoctrineOrmMappingsPassTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php b/tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php index 96b2ea118..ae992a50b 100644 --- a/tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php +++ b/tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php @@ -7,12 +7,14 @@ use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass; use Doctrine\Bundle\DoctrineBundle\Tests\TestCase; use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\Driver\AttributeDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use PHPUnit\Framework\Attributes\IgnoreDeprecations; use Symfony\Component\DependencyInjection\ContainerBuilder; use function assert; +use function interface_exists; use function realpath; class DoctrineOrmMappingsPassTest extends TestCase @@ -40,6 +42,10 @@ public function testCreateAnnotationMappingDriverIsDeprecated(): void public function testAttributeDriverIsRegistered(): void { + if (! interface_exists(EntityManagerInterface::class)) { + self::markTestSkipped('This test requires ORM'); + } + $driverNamespace = 'DoctrineBundle\Entity'; $container = $this->createXmlBundleTestContainer( static function (ContainerBuilder $containerBuilder) use ($driverNamespace): void { From 0653fd12b1eb81e2f6e1a3b4493c9f146fc27832 Mon Sep 17 00:00:00 2001 From: Bob van de Vijver Date: Fri, 23 Jan 2026 15:56:17 +0100 Subject: [PATCH 4/4] Add comment to which ORM version is being checked for --- src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php b/src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php index cce03485d..2ec6cb7ad 100644 --- a/src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php +++ b/src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php @@ -180,6 +180,8 @@ public static function createAnnotationMappingDriver(array $namespaces, array $d public static function createAttributeMappingDriver(array $namespaces, array $directories, array $managerParameters = [], $enabledParameter = false, array $aliasMap = [], bool $reportFieldsWhereDeclared = false) { $driverArgs = [$directories]; + + // Add additional args for ORM <3.0 if (method_exists(AttributeDriver::class, 'getReader')) { $driverArgs[] = $reportFieldsWhereDeclared; }