From 7ad480471d5ec1e6affcce50d6e3f9a40a0b636e Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 1 Feb 2017 15:30:27 +0100 Subject: [PATCH] Change serialization of inheritance fields --- .../ODM/MongoDB/Mapping/ClassMetadata.php | 2 +- .../Tests/Mapping/ClassMetadataTest.php | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php index 500f30df77..7a8b889af8 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php @@ -123,7 +123,7 @@ public function __sleep() $serialized[] = 'customRepositoryClassName'; } - if ($this->inheritanceType != self::INHERITANCE_TYPE_NONE) { + if ($this->inheritanceType != self::INHERITANCE_TYPE_NONE || $this->discriminatorField !== null) { $serialized[] = 'inheritanceType'; $serialized[] = 'discriminatorField'; $serialized[] = 'discriminatorValue'; diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php index 8fb47b8632..5b6a829a8b 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php @@ -4,6 +4,7 @@ use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo; +use Documents\CmsUser; class ClassMetadataTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest { @@ -205,4 +206,37 @@ public function testMapNotExistingFieldThrowsException() $cm = new ClassMetadata('Documents\CmsUser'); $cm->mapField(array('fieldName' => 'namee', 'columnName' => 'name', 'type' => 'string')); } + + /** + * @param ClassMetadata $cm + * @dataProvider dataProviderMetadataClasses + */ + public function testEmbeddedDocumentWithDiscriminator(ClassMetadata $cm) + { + $cm->setDiscriminatorField('discriminator'); + $cm->setDiscriminatorValue('discriminatorValue'); + + $serialized = serialize($cm); + $cm = unserialize($serialized); + + $this->assertSame('discriminator', $cm->discriminatorField); + $this->assertSame('discriminatorValue', $cm->discriminatorValue); + } + + public static function dataProviderMetadataClasses() + { + $document = new ClassMetadata(CmsUser::class); + + $embeddedDocument = new ClassMetadata(CmsUser::class); + $embeddedDocument->isEmbeddedDocument = true; + + $mappedSuperclass = new ClassMetadata(CmsUser::class); + $mappedSuperclass->isMappedSuperclass = true; + + return [ + 'document' => [$document], + 'mappedSuperclass' => [$mappedSuperclass], + 'embeddedDocument' => [$embeddedDocument], + ]; + } }