Skip to content

Commit bfd101b

Browse files
authored
Merge pull request doctrine#1503 from alcaeus/fix-non-null-empty-ids
Allow creating references to "empty" identifiers
2 parents 03bdd75 + 6680aef commit bfd101b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/Doctrine/ODM/MongoDB/DocumentManager.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ public function createDBRef($document, array $referenceMapping = null)
675675
$class = $this->getClassMetadata(get_class($document));
676676
$id = $this->unitOfWork->getDocumentIdentifier($document);
677677

678-
if ( ! $id) {
678+
if ($id === null) {
679679
throw new \RuntimeException(
680680
sprintf('Cannot create a DBRef for class %s without an identifier. Have you forgotten to persist/merge the document first?', $class->name)
681681
);

tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,17 @@ public function testCannotCreateDbRefWithoutId()
196196
$this->dm->createDBRef($d);
197197
}
198198

199+
public function testCreateDbRefWithNonNullEmptyId()
200+
{
201+
$phonenumber = new \Documents\CmsPhonenumber();
202+
$phonenumber->phonenumber = 0;
203+
$this->dm->persist($phonenumber);
204+
205+
$dbRef = $this->dm->createDBRef($phonenumber);
206+
207+
$this->assertSame(array('$ref' => 'CmsPhonenumber', '$id' => 0, '$db' => DOCTRINE_MONGODB_DATABASE), $dbRef);
208+
}
209+
199210
/**
200211
* @expectedException \Doctrine\ODM\MongoDB\Mapping\MappingException
201212
* @expectedExceptionMessage Simple reference must not target document using Single Collection Inheritance, Documents\Tournament\Participant targeted.

0 commit comments

Comments
 (0)