Browse files

[MODM-29] Fixing issue with forcing $set strategy.

  • Loading branch information...
1 parent c8bdcaf commit 485fbdfb7f85ecb290530ea0fc9c55a6dca91e19 @jwage jwage committed Aug 18, 2010
View
22 lib/Doctrine/ODM/MongoDB/Persisters/BasicDocumentPersister.php
@@ -430,20 +430,20 @@ public function prepareUpdateData($document)
$new = isset($changeset[$mapping['fieldName']][1]) ? $changeset[$mapping['fieldName']][1] : null;
if ($mapping['type'] === 'many' || $mapping['type'] === 'collection') {
- if (isset($mapping['embedded']) && $new) {
- foreach ($new as $k => $v) {
- if ( ! isset($old[$k])) {
- continue;
- }
- $update = $this->prepareUpdateData($v);
- foreach ($update as $cmd => $values) {
- foreach ($values as $key => $value) {
- $result[$cmd][$mapping['fieldName'] . '.' . $k . '.' . $key] = $value;
+ if ($mapping['strategy'] === 'pushPull') {
+ if (isset($mapping['embedded']) && $new) {
+ foreach ($new as $k => $v) {
+ if ( ! isset($old[$k])) {
+ continue;
+ }
+ $update = $this->prepareUpdateData($v);
+ foreach ($update as $cmd => $values) {
+ foreach ($values as $key => $value) {
+ $result[$cmd][$mapping['fieldName'] . '.' . $k . '.' . $key] = $value;
+ }
}
}
}
- }
- if ($mapping['strategy'] === 'pushPull') {
if ($old !== $new) {
$old = $old ? $old : array();
$new = $new ? $new : array();
View
72 tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM29Test.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;
+
+require_once __DIR__ . '/../../../../../../TestInit.php';
+
+class MODM29Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
+{
+ public function testTest()
+ {
+ $collection = new \Doctrine\Common\Collections\ArrayCollection(array(
+ new MODM29Embedded('0'),
+ new MODM29Embedded('1'),
+ new MODM29Embedded('2')
+ ));
+
+ // TEST CASE:
+ $doc = new MODM29Doc($collection);
+
+ $this->dm->persist($doc);
+ $this->dm->flush();
+
+ // place element '0' after '1'
+ $collection = new \Doctrine\Common\Collections\ArrayCollection(array(
+ $collection[1],
+ $collection[0],
+ $collection[2]
+ ));
+
+ $doc->set($collection);
+
+ // changing value together with reordering causes issue when saving:
+ $collection[1]->set('tmp');
+
+ $this->dm->persist($doc);
+ $this->dm->flush(array('safe' => true));
+
+ $this->dm->refresh($doc);
+
+ $array = array();
+ foreach($doc->get() as $value) {
+ $array[] = $value->get();
+ }
+ $this->assertEquals(array('1', 'tmp', '2'), $array);
+ }
+}
+
+/** @Document(collection="tests", db="tests") */
+class MODM29Doc
+{
+ /** @Id */
+ protected $id;
+
+ /** @EmbedMany(targetDocument="MODM29Embedded", strategy="set", cascade="all") */
+ protected $collection;
+
+ function __construct($c) {$this->set($c);}
+
+ function set($c) {$this->collection = $c;}
+ function get() {return $this->collection;}
+}
+
+/** @EmbeddedDocument */
+class MODM29Embedded
+{
+ /** @String */
+ protected $val;
+
+ function __construct($val) {$this->set($val);}
+ function get() {return $this->val;}
+ function set($val) {$this->val = $val;}
+}

0 comments on commit 485fbdf

Please sign in to comment.