Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[MODM-56] Fixing issue with recomputing a single documents changes wh…

…en document has a preUpdate lifecycle callback
  • Loading branch information...
commit faf85a281b1562d184b44261a6e4dcdb13e03c5e 1 parent 84f34e6
@jwage jwage authored
View
16 lib/Doctrine/ODM/MongoDB/UnitOfWork.php
@@ -457,7 +457,9 @@ public function computeChangeSet($parentDocument, Mapping\ClassMetadata $class,
} else if ($isChangeTrackingNotify) {
continue;
} else if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'many') {
- $changeSet[$propName] = array($orgValue, $actualValue);
+ if ($orgValue !== $actualValue) {
+ $changeSet[$propName] = array($orgValue, $actualValue);
+ }
} else if (is_object($orgValue) && $orgValue !== $actualValue) {
$changeSet[$propName] = array($orgValue, $actualValue);
} else if ($orgValue != $actualValue || ($orgValue === null ^ $actualValue === null)) {
@@ -609,16 +611,10 @@ public function recomputeSingleDocumentChangeSet($class, $document)
$class = $this->dm->getClassMetadata(get_class($document));
}
- $actualData = array();
- foreach ($class->reflFields as $name => $refProp) {
- if ( ! $class->isIdentifier($name)) {
- $actualData[$name] = $refProp->getValue($document);
- }
- }
+ $actualData = $this->getDocumentActualData($document);
$originalData = $this->originalDocumentData[$oid];
$changeSet = array();
-
foreach ($actualData as $propName => $actualValue) {
$orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null;
if ($orgValue instanceof PersistentCollection) {
@@ -633,7 +629,9 @@ public function recomputeSingleDocumentChangeSet($class, $document)
$changeSet[$propName] = array($orgValue, $actualValue);
}
} else if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'many') {
- $changeSet[$propName] = array($orgValue, $actualValue);
+ if ($orgValue !== $actualValue) {
+ $changeSet[$propName] = array($orgValue, $actualValue);
+ }
} else if (is_object($orgValue) && $orgValue !== $actualValue) {
$changeSet[$propName] = array($orgValue, $actualValue);
} else if ($orgValue != $actualValue || ($orgValue === null ^ $actualValue === null)) {
View
72 tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM56Test.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;
+
+require_once __DIR__ . '/../../../../../../TestInit.php';
+
+class MODM56Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
+{
+ public function testTest()
+ {
+ $parent = new MODM56Parent('Parent');
+ $this->dm->persist($parent);
+ $this->dm->flush();
+
+ $childOne = new MODM56Child('Child One');
+ $parent->children[] = $childOne;
+
+ $childTwo = new MODM56Child('Child Two');
+ $parent->children[] = $childTwo;
+ $this->dm->flush(array('safe' => true));
+
+ $test = $this->dm->getDocumentCollection(__NAMESPACE__.'\MODM56Parent')->findOne();
+
+ $this->assertEquals('Parent', $test['name']);
+ $this->assertInstanceOf('\MongoDate', $test['updatedAt']);
+ $this->assertEquals(2, count($test['children']));
+ $this->assertEquals('Child One', $test['children'][0]['name']);
+ $this->assertEquals('Child Two', $test['children'][1]['name']);
+ }
+}
+
+/** @Document @HasLifecycleCallbacks */
+class MODM56Parent
+{
+ /** @Id */
+ public $id;
+
+ /** @String */
+ public $name;
+
+ /** @Date */
+ public $updatedAt;
+
+ /** @EmbedMany(targetDocument="MODM56Child") */
+ public $children = array();
+
+ public function __construct($name)
+ {
+ $this->name = $name;
+ }
+
+ /** @PreUpdate */
+ public function preUpdate()
+ {
+ $this->updatedAt = new \DateTime();
+ }
+}
+
+/** @EmbeddedDocument */
+class MODM56Child
+{
+ /** @Id */
+ public $id;
+
+ /** @String */
+ public $name;
+
+ public function __construct($name)
+ {
+ $this->name = $name;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.