From 0b9c1c7555c6f7d359ddfbab02a225d0785cb127 Mon Sep 17 00:00:00 2001 From: parada85 Date: Fri, 17 Jan 2025 11:02:37 +0100 Subject: [PATCH] Fix discriminatorValue key 0 (#2716) --- .../Mapping/Annotations/DiscriminatorMap.php | 4 +- .../MongoDB/Persisters/DocumentPersister.php | 2 +- .../Tests/Functional/Ticket/GH2158Test.php | 46 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 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..644e244432 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DiscriminatorMap.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DiscriminatorMap.php @@ -16,10 +16,10 @@ #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)] final class DiscriminatorMap implements Annotation { - /** @var array */ + /** @var array */ public $value; - /** @param array $value */ + /** @param array $value */ public function __construct(array $value) { $this->value = $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 +{ +}