diff --git a/lib/Doctrine/ODM/MongoDB/DocumentManager.php b/lib/Doctrine/ODM/MongoDB/DocumentManager.php index 7b7841b6d1..9d3a710cbe 100644 --- a/lib/Doctrine/ODM/MongoDB/DocumentManager.php +++ b/lib/Doctrine/ODM/MongoDB/DocumentManager.php @@ -701,8 +701,10 @@ public function createDBRef($document, array $referenceMapping = null) $dbRef = array( '$ref' => $class->getCollection(), '$id' => $class->getDatabaseIdentifierValue($id), - '$db' => $this->getDocumentDatabase($class->name)->getName(), ); + if ( empty($referenceMapping['partial'])) { + $dbRef['$db'] = $this->getDocumentDatabase($class->name)->getName(); + } if ($class->hasDiscriminator()) { $dbRef[$class->discriminatorField] = $class->discriminatorValue; diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/ReferenceMany.php b/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/ReferenceMany.php index b12aa1c644..200d198b91 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/ReferenceMany.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/ReferenceMany.php @@ -25,6 +25,7 @@ final class ReferenceMany extends AbstractField public $type = 'many'; public $reference = true; public $simple = false; + public $partial = false; public $targetDocument; public $discriminatorField; public $discriminatorMap; diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/ReferenceOne.php b/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/ReferenceOne.php index a4914223e7..8731be6ad4 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/ReferenceOne.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/ReferenceOne.php @@ -25,6 +25,7 @@ final class ReferenceOne extends AbstractField public $type = 'one'; public $reference = true; public $simple = false; + public $partial = false; public $targetDocument; public $discriminatorField; public $discriminatorMap; diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Persisters/PersistenceBuilderTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Persisters/PersistenceBuilderTest.php index 9810608c53..12dfffbdc9 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Persisters/PersistenceBuilderTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Persisters/PersistenceBuilderTest.php @@ -10,6 +10,7 @@ use Documents\CmsComment; use Documents\Functional\SameCollection1; use Documents\Functional\SameCollection2; +use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; class PersistenceBuilderTest extends BaseTest { @@ -296,4 +297,38 @@ public function testAdvancedQueriesOnReferenceWithDiscriminatorMap() $this->assertEquals($articleId, $singleResult->article->id); $this->assertEquals(1, $results->count(true)); } + + public function testReferencePartial() + { + $article = new CmsArticle(); + $article->id = new \MongoId(); + + $this->dm->persist($article); + $this->dm->flush(); + + $test = new DocumentWithPartialReference(); + $test->article = $article; + $this->dm->persist($test); + + $this->uow->computeChangeSets(); + + $expectedData = array( + 'article' => array( + '$id' => $test->article->id, + '$ref' => 'CmsArticle' + ) + ); + + $this->assertDocumentInsertData($expectedData, $this->pb->prepareInsertData($test)); + } +} + +/** @ODM\Document */ +class DocumentWithPartialReference +{ + /** @ODM\Id */ + public $id; + + /** @ODM\ReferenceOne(targetDocument="Documents\CmsComment", partial="true") */ + public $article; }