Skip to content

Commit

Permalink
Merge pull request #1636 from Nilz11/fix-dbref
Browse files Browse the repository at this point in the history
Fixes dbref #1635
  • Loading branch information
alcaeus authored Sep 28, 2017
2 parents d1fed2d + 7fce5d1 commit 64fa670
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ private function prepareDbRefElement($fieldName, $value, array $mapping, $inNewO
return [[$fieldName, $dbRef]];
}
$keys = ['$ref' => true, '$id' => true, '$db' => true];
if ($mapping['storeAs'] === ClassMetadataInfo::REFERENCE_STORE_AS_ID) {
if ($mapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB) {
unset($keys['$db']);
}
if (isset($mapping['targetDocument'])) {
Expand Down
22 changes: 22 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/DocumentRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ public function testFindByRefOneWithoutTargetDocumentFull()
$this->assertSame($account, $this->dm->getRepository(Account::class)->findOneBy(['user' => $user]));
}

public function testFindByRefOneWithoutTargetDocumentStoredAsDbRef()
{
$user = new User();
$account = new Account('name');
$account->setUserDbRef($user);
$this->dm->persist($user);
$this->dm->persist($account);
$this->dm->flush();

$query = $this->dm
->getUnitOfWork()
->getDocumentPersister(Account::class)
->prepareQueryOrNewObj(['userDbRef' => $user]);
$expectedQuery = [
'userDbRef.$ref' => 'users',
'userDbRef.$id' => new \MongoId($user->getId())
];
$this->assertEquals($expectedQuery, $query);

$this->assertSame($account, $this->dm->getRepository(Account::class)->findOneBy(['userDbRef' => $user]));
}

public function testFindDiscriminatedByRefManyFull()
{
$project = new SubProject('mongodb-odm');
Expand Down
13 changes: 13 additions & 0 deletions tests/Documents/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Account
/** @ODM\ReferenceOne */
protected $user;

/** @ODM\ReferenceOne(storeAs="dbRef") */
protected $userDbRef;

public function getId()
{
return $this->id;
Expand All @@ -40,4 +43,14 @@ public function getUser()
{
return $this->user;
}

public function setUserDbRef($userDbRef)
{
$this->userDbRef = $userDbRef;
}

public function getUserDbRef()
{
return $this->userDbRef;
}
}

0 comments on commit 64fa670

Please sign in to comment.