Skip to content

Commit

Permalink
Fix discriminatorValue key 0 (#2716)
Browse files Browse the repository at this point in the history
  • Loading branch information
parada85 authored Jan 17, 2025
1 parent 27002d1 commit 0b9c1c7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)]
final class DiscriminatorMap implements Annotation
{
/** @var array<string, class-string> */
/** @var array<class-string> */
public $value;

/** @param array<string, class-string> $value */
/** @param array<class-string> $value */
public function __construct(array $value)
{
$this->value = $value;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
46 changes: 46 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2158Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;

class GH2158Test extends BaseTestCase
{
public function testDiscriminatorMapCreationType(): void
{
$obj = new GH2158FirstType();
$this->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
{
}

0 comments on commit 0b9c1c7

Please sign in to comment.