Browse files

[MODM-45] Fixing issue with storing null values and empty arrays.

  • Loading branch information...
1 parent 6017938 commit b3a16496c0576e8f926faec9bbcfd5716006273b @jwage jwage committed Aug 17, 2010
View
19 lib/Doctrine/ODM/MongoDB/Persisters/BasicDocumentPersister.php
@@ -374,10 +374,10 @@ public function prepareInsertData($document)
continue;
}
$value = $this->prepareValue($mapping, $new);
- // Don't store empty arrays
- if (is_array($value) && empty($value)) {
+ if ($value === null && $mapping['nullable'] === false) {
continue;
}
+
$insertData[$mapping['fieldName']] = $value;
if (isset($mapping['reference'])) {
$scheduleForUpdate = false;
@@ -530,12 +530,14 @@ private function prepareValue(array $mapping, $value)
}
if ($mapping['type'] === 'many') {
$prepared = array();
-
$oneMapping = $mapping;
$oneMapping['type'] = 'one';
foreach ($value as $rawValue) {
$prepared[] = $this->prepareValue($oneMapping, $rawValue);
}
+ if (empty($prepared)) {
+ $prepared = null;
+ }
} elseif (isset($mapping['reference']) || isset($mapping['embedded'])) {
if (isset($mapping['embedded'])) {
$prepared = $this->prepareEmbeddedDocValue($mapping, $value);
@@ -609,24 +611,29 @@ private function prepareEmbeddedDocValue(array $embeddedMapping, $embeddedDocume
foreach ($rawValue as $embeddedDoc) {
$value[] = $this->prepareEmbeddedDocValue($mapping, $embeddedDoc);
}
+ if (empty($value)) {
+ $value = null;
+ }
} elseif ($mapping['type'] == 'one') {
$value = $this->prepareEmbeddedDocValue($mapping, $rawValue);
}
} elseif (isset($mapping['reference'])) {
if ($mapping['type'] == 'many') {
- $value = array();
+ $value = array();
foreach ($rawValue as $referencedDoc) {
$value[] = $this->prepareReferencedDocValue($mapping, $referencedDoc);
}
+ if (empty($value)) {
+ $value = null;
+ }
} else {
$value = $this->prepareReferencedDocValue($mapping, $rawValue);
}
}
} else {
$value = Type::getType($mapping['type'])->convertToDatabaseValue($rawValue);
}
- // Don't store empty arrays
- if (is_array($value) && empty($value)) {
+ if ($value === null && $mapping['nullable'] === false) {
continue;
}
$embeddedDocumentValue[$mapping['fieldName']] = $value;
View
48 tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM45Test.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;
+
+require_once __DIR__ . '/../../../../../../TestInit.php';
+
+class MODM45Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
+{
+ public function testTest()
+ {
+ $a = new a();
+ $a->setB(new b());
+
+ $this->dm->persist($a);
+ $this->dm->flush();
+ $this->dm->clear();
+
+ $a = $this->dm->loadByID(__NAMESPACE__.'\a', $a->getId());
+ $c = (null !== $a->getB());
+ $this->assertTrue($c); // returns false, while expecting true
+ }
+}
+
+/** @Document(collection="modm45_test") */
+class a
+{
+ /** @Id */
+ protected $id;
+
+ /** @String */
+ protected $tmp = 'WorkaroundToBeSaved';
+
+ /** @EmbedOne(targetDocument="b", cascade="all") */
+ protected $b;
+
+ function getId() {return $this->id;}
+ function getB() {return $this->b;}
+ function setB($b) {$this->b = $b;}
+}
+
+/** @EmbeddedDocument */
+class b
+{
+ /** @String */
+ protected $val;
+ function setVal($val) {$this->val = $val;}
+ function getVal() {return $this->val;}
+}

0 comments on commit b3a1649

Please sign in to comment.