From 0e2607d05eba8d3a8b8f6c94ad4a88ac58cc6b97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 30 Oct 2025 15:47:10 +0100 Subject: [PATCH] Deprecate ClassMetadataFactoryInterface::getProxyClassNameResolver() --- src/DocumentManager.php | 10 ++++++++-- src/DocumentNotFoundException.php | 2 +- src/Events.php | 2 +- src/Mapping/ClassMetadataFactoryInterface.php | 2 ++ tests/Tests/DocumentManagerTest.php | 9 +++++++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/DocumentManager.php b/src/DocumentManager.php index f73be994d8..2dfb678741 100644 --- a/src/DocumentManager.php +++ b/src/DocumentManager.php @@ -176,7 +176,9 @@ public function resolveClassName(string $className): string $this->metadataFactory = new $metadataFactoryClassName(); $this->metadataFactory->setDocumentManager($this); $this->metadataFactory->setConfiguration($this->config); - $this->metadataFactory->setProxyClassNameResolver($this->classNameResolver); + if (! $this->config->isNativeLazyObjectEnabled()) { + $this->metadataFactory->setProxyClassNameResolver($this->classNameResolver); + } $cacheDriver = $this->config->getMetadataCache(); if ($cacheDriver) { @@ -310,10 +312,14 @@ public function getSchemaManager(): SchemaManager /** * Returns the class name resolver which is used to resolve real class names for proxy objects. * - * @deprecated Fetch metadata for any class string (e.g. proxy object class) and read the class name from the metadata object + * @deprecated Since 2.15, the use of proxy classes is deprecated and will be removed in Doctrine ODM 3.0. */ public function getClassNameResolver(): ClassNameResolver { + if ($this->getConfiguration()->isNativeLazyObjectEnabled()) { + trigger_deprecation('doctrine/mongodb-odm', '2.15', 'The %s() method is deprecated and will be removed in Doctrine ODM 3.0. There are no proxy classes when using native lazy objects', __METHOD__); + } + return $this->classNameResolver; } diff --git a/src/DocumentNotFoundException.php b/src/DocumentNotFoundException.php index 310cac6de9..a32c9965f6 100644 --- a/src/DocumentNotFoundException.php +++ b/src/DocumentNotFoundException.php @@ -12,7 +12,7 @@ use const JSON_THROW_ON_ERROR; /** - * Class for exception when encountering proxy object that has + * Class for exception when encountering a lazy object that has * an identifier that does not exist in the database. */ final class DocumentNotFoundException extends MongoDBException diff --git a/src/Events.php b/src/Events.php index bbeb6e4d84..b19c7f3afa 100644 --- a/src/Events.php +++ b/src/Events.php @@ -130,7 +130,7 @@ private function __construct() public const onClear = 'onClear'; /** - * The documentNotFound event occurs if a proxy object could not be found in + * The documentNotFound event occurs if a lazy object could not be found in * the database. */ public const documentNotFound = 'documentNotFound'; diff --git a/src/Mapping/ClassMetadataFactoryInterface.php b/src/Mapping/ClassMetadataFactoryInterface.php index 3c543e3cc0..bacd47ec3d 100644 --- a/src/Mapping/ClassMetadataFactoryInterface.php +++ b/src/Mapping/ClassMetadataFactoryInterface.php @@ -35,6 +35,8 @@ public function setDocumentManager(DocumentManager $dm): void; /** * Sets a resolver for real class names of a proxy. + * + * @deprecated This method is deprecated and will be removed in Doctrine ODM 3.0. */ public function setProxyClassNameResolver(ProxyClassNameResolver $resolver): void; } diff --git a/tests/Tests/DocumentManagerTest.php b/tests/Tests/DocumentManagerTest.php index 832037e53c..a23f5612d8 100644 --- a/tests/Tests/DocumentManagerTest.php +++ b/tests/Tests/DocumentManagerTest.php @@ -14,6 +14,7 @@ use Doctrine\ODM\MongoDB\Mapping\MappingException; use Doctrine\ODM\MongoDB\MongoDBException; use Doctrine\ODM\MongoDB\Proxy\Factory\ProxyFactory; +use Doctrine\ODM\MongoDB\Proxy\Resolver\ClassNameResolver; use Doctrine\ODM\MongoDB\Query\Builder as QueryBuilder; use Doctrine\ODM\MongoDB\Query\FilterCollection; use Doctrine\ODM\MongoDB\SchemaManager; @@ -34,6 +35,7 @@ use MongoDB\BSON\ObjectId; use MongoDB\Client; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\IgnoreDeprecations; use RuntimeException; use stdClass; @@ -261,6 +263,13 @@ public function testGetClassNameForAssociationReturnsTargetDocumentWithNullData( $mapping = ClassMetadataTestUtil::getFieldMapping(['targetDocument' => User::class]); self::assertEquals(User::class, $this->dm->getClassNameForAssociation($mapping, null)); } + + #[IgnoreDeprecations] + public function testGetClassNameResolver(): void + { + $resolver = $this->dm->getClassNameResolver(); + self::assertInstanceOf(ClassNameResolver::class, $resolver); + } } #[ODM\Document]