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

Partial reference support (without $db property) #807

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Copy link
Member

Choose a reason for hiding this comment

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

Remove space before empty(

$dbRef['$db'] = $this->getDocumentDatabase($class->name)->getName();
Copy link
Member

Choose a reason for hiding this comment

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

Not always setting a $db key can cause errors in Expr::references() and Expr::includesReferenceTo() when using the methods with a reference that has the partial flag enabled and no targetDocument set. I'm not sure how much of a use case this is, but either the Expr class or the mapping drivers need to make sure to handle this case.

Copy link
Member

Choose a reason for hiding this comment

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

definitely 👍 for catching this

}

if ($class->hasDiscriminator()) {
$dbRef[$class->discriminatorField] = $class->discriminatorValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}