Skip to content

Commit

Permalink
Use storeAs=dbRef as the new default setting for references
Browse files Browse the repository at this point in the history
  • Loading branch information
coudenysj authored and alcaeus committed Nov 3, 2017
1 parent 547c7c9 commit fa8edc9
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 22 deletions.
11 changes: 6 additions & 5 deletions docs/en/reference/reference-mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -358,16 +358,17 @@ reduces the amount of storage used).

The ``storeAs`` option has the following possible values:

- **dbRefWithDb**: Uses a `DBRef`_ with ``$ref``, ``$id``, and ``$db`` fields (this is the default)
- **dbRef**: Uses a `DBRef`_ with ``$ref`` and ``$id``
- **dbRefWithDb**: Uses a `DBRef`_ with ``$ref``, ``$id``, and ``$db`` fields
- **dbRef**: Uses a `DBRef`_ with ``$ref`` and ``$id`` (this is the default)
- **ref**: Uses a custom embedded object with an ``id`` field
- **id**: Uses the identifier of the referenced object

.. note::

For backwards compatibility ``storeAs=dbRefWithDb`` is the default, but
   ``storeAs=ref`` is the recommended setting.

Up until 2.0 ``storeAs=dbRefWithDb`` was the default setting. If you have data in
the old format, you should add ``storeAs=dbRefWithDb`` to all your references, or
update the database references (deleting the ``$db`` field) as ``storeAs=dbRef``
is the new default setting.

Cascading Operations
--------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class ReferenceMany extends AbstractField
{
public $type = 'many';
public $reference = true;
public $storeAs = ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB;
public $storeAs = ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF;
public $targetDocument;
public $discriminatorField;
public $discriminatorMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class ReferenceOne extends AbstractField
{
public $type = 'one';
public $reference = true;
public $storeAs = ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB;
public $storeAs = ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF;
public $targetDocument;
public $discriminatorField;
public $discriminatorMap;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type
'orphanRemoval' => isset($attributes['orphan-removal']) ? ('true' === (string) $attributes['orphan-removal']) : false,
'type' => $type,
'reference' => true,
'storeAs' => isset($attributes['store-as']) ? (string) $attributes['store-as'] : ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB,
'storeAs' => isset($attributes['store-as']) ? (string) $attributes['store-as'] : ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF,
'targetDocument' => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
'collectionClass' => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null,
'name' => (string) $attributes['field'],
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private function addMappingFromReference(ClassMetadataInfo $class, $fieldName, $
'orphanRemoval' => isset($reference['orphanRemoval']) ? $reference['orphanRemoval'] : false,
'type' => $type,
'reference' => true,
'storeAs' => isset($reference['storeAs']) ? (string) $reference['storeAs'] : ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB,
'storeAs' => isset($reference['storeAs']) ? (string) $reference['storeAs'] : ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF,
'targetDocument' => isset($reference['targetDocument']) ? $reference['targetDocument'] : null,
'collectionClass' => isset($reference['collectionClass']) ? $reference['collectionClass'] : null,
'fieldName' => $fieldName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ public function testPersistingNewDocumentWithOnlyOneReference()

$this->assertEquals('servers', $test['server']['$ref']);
$this->assertTrue(isset($test['server']['$id']));
$this->assertEquals(DOCTRINE_MONGODB_DATABASE, $test['server']['$db']);
$this->assertEquals('server_guest', $test['server']['_doctrine_class_name']);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function testDriver()
'name' => 'account',
'type' => 'one',
'reference' => true,
'storeAs' => ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB,
'storeAs' => ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF,
'targetDocument' => 'Documents\Account',
'collectionClass' => null,
'cascade' => array('remove', 'persist', 'refresh', 'merge', 'detach'),
Expand Down Expand Up @@ -202,7 +202,7 @@ public function testDriver()
'name' => 'groups',
'type' => 'many',
'reference' => true,
'storeAs' => ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB,
'storeAs' => ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF,
'targetDocument' => 'Documents\Group',
'collectionClass' => null,
'cascade' => array('remove', 'persist', 'refresh', 'merge', 'detach'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testDriverShouldParseNonStringAttributes()
$this->assertSame(true, $profileMapping['orphanRemoval']);

$profileMapping = $classMetadata->fieldMappings['groups'];
$this->assertSame(ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB, $profileMapping['storeAs']);
$this->assertSame(ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF, $profileMapping['storeAs']);
$this->assertSame(false, $profileMapping['orphanRemoval']);
$this->assertSame(0, $profileMapping['limit']);
$this->assertSame(2, $profileMapping['skip']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ public function testPrepareInsertDataWithCreatedReferenceOne()

$expectedData = array(
'article' => array(
'$db' => DOCTRINE_MONGODB_DATABASE,
'$id' => new \MongoId($article->id),
'$ref' => 'CmsArticle'
)
Expand All @@ -172,7 +171,6 @@ public function testPrepareInsertDataWithFetchedReferenceOne()

$expectedData = array(
'article' => array(
'$db' => DOCTRINE_MONGODB_DATABASE,
'$id' => new \MongoId($article->id),
'$ref' => 'CmsArticle'
)
Expand Down Expand Up @@ -202,7 +200,6 @@ public function testPrepareUpsertData()
'topic' => 'test',
'text' => 'text',
'article' => array(
'$db' => DOCTRINE_MONGODB_DATABASE,
'$id' => new \MongoId($article->id),
'$ref' => 'CmsArticle'
),
Expand Down
10 changes: 5 additions & 5 deletions tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function testReferencesStoreAsDbRef()
$this->assertSame($kris, $query->getSingleResult());
}

public function testIncludesReferenceTo()
public function testIncludesReferenceToWithStoreAsDbRefWithDb()
{
$kris = new Person('Kris');
$jon = new Person('Jon');
Expand Down Expand Up @@ -380,22 +380,22 @@ class Person
/** @ODM\Field(type="string") */
public $firstName;

/** @ODM\ReferenceOne */
/** @ODM\ReferenceOne(storeAs="dbRefWithDb") */
public $bestFriend;

/** @ODM\ReferenceOne(storeAs="id", targetDocument="Doctrine\ODM\MongoDB\Tests\Person") */
public $bestFriendSimple;

/** @ODM\ReferenceOne(storeAs="dbRef") */
/** @ODM\ReferenceOne */
public $bestFriendPartial;

/** @ODM\ReferenceMany */
/** @ODM\ReferenceMany(storeAs="dbRefWithDb") */
public $friends = array();

/** @ODM\ReferenceMany(storeAs="id", targetDocument="Doctrine\ODM\MongoDB\Tests\Person") */
public $friendsSimple = array();

/** @ODM\ReferenceMany(storeAs="dbRef") */
/** @ODM\ReferenceMany */
public $friendsPartial = array();

/** @ODM\EmbedOne(targetDocument="Pet") */
Expand Down
2 changes: 1 addition & 1 deletion tests/Documents/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Account
/** @ODM\Field */
private $name;

/** @ODM\ReferenceOne */
/** @ODM\ReferenceOne(storeAs="dbRefWithDb") */
protected $user;

/** @ODM\ReferenceOne(storeAs="dbRef") */
Expand Down

0 comments on commit fa8edc9

Please sign in to comment.