From 15ad85de6fe57f786c455fc3dfc6f42762f93106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20=C3=81ngel=20Parada?= Date: Fri, 10 Jan 2025 13:19:10 +0100 Subject: [PATCH] Fix discriminatorValue 0 --- .../Mapping/Annotations/DiscriminatorMap.php | 2 +- .../MongoDB/Persisters/DocumentPersister.php | 2 +- .../Tests/Functional/Ticket/GH2158Test.php | 46 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2158Test.php diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DiscriminatorMap.php b/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DiscriminatorMap.php index cd1131a25d..46947e8952 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DiscriminatorMap.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DiscriminatorMap.php @@ -16,7 +16,7 @@ #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)] final class DiscriminatorMap implements Annotation { - /** @var array */ + /** @var array */ public $value; /** @param array $value */ diff --git a/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php b/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php index 780c0c6004..061b0c9da0 100644 --- a/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php +++ b/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php @@ -1470,7 +1470,7 @@ private function getClassDiscriminatorValues(ClassMetadata $metadata): array foreach ($metadata->subClasses as $className) { $key = array_search($className, $metadata->discriminatorMap); - if (! $key) { + if ($key === false) { continue; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2158Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2158Test.php new file mode 100644 index 0000000000..f01456ce21 --- /dev/null +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2158Test.php @@ -0,0 +1,46 @@ +dm->persist($obj); + $this->dm->flush(); + + self::assertEquals($this->dm->find(GH2158Abstract::class, $obj->getId()), $obj); + } +} + +#[ODM\Document(collection: 'documents')] +#[ODM\InheritanceType('SINGLE_COLLECTION')] +#[ODM\DiscriminatorField('type')] +#[ODM\DiscriminatorMap([0 => GH2158FirstType::class, 1 => GH2158SecondType::class])] +abstract class GH2158Abstract +{ + /** @var string */ + #[ODM\Id] + protected $id; + + public function getId(): string + { + return $this->id; + } +} + +#[ODM\Document] +class GH2158FirstType extends GH2158Abstract +{ +} + +#[ODM\Document] +class GH2158SecondType extends GH2158Abstract +{ +}