Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change serialization of inheritance fields #1556

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be better to check for being embedded document's metadata? Otherwise the issue still persists when not using discriminatorField which I think can be null to default to _doctrine_class_name later down the line? Please take this with a grain of salt though, I'm always confused by class metadata's defaults

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, didn't think about _doctrine_class_name - especially since that doesn't involve a map either. Will have to investigate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think that map can be combined with default discriminator field since it only provides string <=> FQCN mapping ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking at the code, this looks safe: _doctrine_class_name is not used at the class level, which this PR is trying to fix. Instead it's used when an association contains no targetDocument and no discriminatorField has been set - in this case the discriminatorField is simply set to _doctrine_class_name as if it were used in the mapping. I think we should be safe merging this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

$serialized[] = 'inheritanceType';
$serialized[] = 'discriminatorField';
$serialized[] = 'discriminatorValue';
Expand Down
34 changes: 34 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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],
];
}
}