Skip to content

Commit

Permalink
Merge branch 'test-gh817' into 1.0.x
Browse files Browse the repository at this point in the history
* test-gh817:
  Inverse side of RefOne is no longer included in change set
  Added failing test for #817
  • Loading branch information
alcaeus committed Dec 23, 2015
2 parents 8c7398f + 6dbaf85 commit 790110e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Persisters/PersistenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function prepareInsertData($document)
$insertData[$mapping['name']] = Type::getType($mapping['type'])->convertToDatabaseValue($new);

// @ReferenceOne
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE && $mapping['isOwningSide']) {
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
$insertData[$mapping['name']] = $this->prepareReferencedDocumentValue($mapping, $new);

// @EmbedOne
Expand Down Expand Up @@ -207,7 +207,7 @@ public function prepareUpdateData($document)
}

// @ReferenceOne
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE && $mapping['isOwningSide']) {
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
if (isset($new) || $mapping['nullable'] === true) {
$updateData['$set'][$mapping['name']] = (is_null($new) ? null : $this->prepareReferencedDocumentValue($mapping, $new));
} else {
Expand Down Expand Up @@ -281,7 +281,7 @@ public function prepareUpsertData($document)
}

// @ReferenceOne
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE && $mapping['isOwningSide']) {
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
if (isset($new) || $mapping['nullable'] === true) {
$updateData['$set'][$mapping['name']] = (is_null($new) ? null : $this->prepareReferencedDocumentValue($mapping, $new));
}
Expand Down
8 changes: 6 additions & 2 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,10 @@ private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $r
$actualData[$propName] = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName);
$actualValue = $actualData[$propName];
}
// ignore inverse side of reference relationship
if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) {
continue;
}
$changeSet[$propName] = array(null, $actualValue);
}
$this->documentChangeSets[$oid] = $changeSet;
Expand Down Expand Up @@ -736,8 +740,8 @@ private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $r
continue;
}

// ignore inverse side of reference-many relationship
if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['type'] === 'many' && $class->fieldMappings[$propName]['isInverseSide']) {
// ignore inverse side of reference relationship
if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) {
continue;
}

Expand Down
41 changes: 41 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Events/LifecycleCallbacksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ public function testReferences()
$this->assertFalse($user2->preUpdate);
$this->assertFalse($user2->postUpdate);
}

public function testEventsNotFiredForInverseSide()
{
$customer = new Customer();
$cart = new Cart();

$this->dm->persist($customer);
$this->dm->persist($cart);
$this->dm->flush();

$customer->cart = $cart;
$cart->customer = $customer;
$this->dm->flush();

$this->assertFalse($customer->postUpdate);
$this->assertTrue($cart->postUpdate);
}
}

/** @ODM\Document */
Expand All @@ -238,6 +255,30 @@ class User extends BaseDocument
public $friends = array();
}

/** @ODM\Document */
class Cart extends BaseDocument
{
/** @ODM\Id */
public $id;

/**
* @ODM\ReferenceOne(targetDocument="Customer", inversedBy="cart")
*/
public $customer;
}

/** @ODM\Document */
class Customer extends BaseDocument
{
/** @ODM\Id */
public $id;

/**
* @ODM\ReferenceOne(targetDocument="Cart", mappedBy="customer")
*/
public $cart;
}

/** @ODM\EmbeddedDocument */
class Profile extends BaseDocument
{
Expand Down

0 comments on commit 790110e

Please sign in to comment.