Skip to content

Commit f80a89d

Browse files
committed
Fix hydrating null value when field is nullable
1 parent 4ac50ee commit f80a89d

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,13 @@ private function generateHydratorClass(ClassMetadata $class, $hydratorClassName,
232232
$code .= sprintf(<<<EOF
233233
234234
/** @Field(type="{$mapping['type']}") */
235-
if (isset(\$data['%1\$s'])) {
235+
if (isset(\$data['%1\$s']) || (! empty(\$this->class->fieldMappings['%2\$s']['nullable']) && array_key_exists('%1\$s', \$data))) {
236236
\$value = \$data['%1\$s'];
237-
%3\$s
237+
if (\$value !== null) {
238+
%3\$s
239+
} else {
240+
\$return = null;
241+
}
238242
\$this->class->reflFields['%2\$s']->setValue(\$document, \$return);
239243
\$hydratedData['%2\$s'] = \$return;
240244
}

tests/Doctrine/ODM/MongoDB/Tests/HydratorTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function testHydrator()
1313
$user = new HydrationClosureUser();
1414
$this->dm->getHydratorFactory()->hydrate($user, array(
1515
'_id' => 1,
16+
'title' => null,
1617
'name' => 'jon',
1718
'birthdate' => new \DateTime('1961-01-01'),
1819
'referenceOne' => array('$id' => '1'),
@@ -31,6 +32,7 @@ public function testHydrator()
3132
));
3233

3334
$this->assertEquals(1, $user->id);
35+
$this->assertSame(null, $user->title);
3436
$this->assertEquals('jon', $user->name);
3537
$this->assertInstanceOf('DateTime', $user->birthdate);
3638
$this->assertInstanceOf(__NAMESPACE__.'\HydrationClosureReferenceOne', $user->referenceOne);
@@ -49,6 +51,9 @@ class HydrationClosureUser
4951
/** @ODM\Id */
5052
public $id;
5153

54+
/** @ODM\Field(type="string", nullable=true) */
55+
public $title = 'Mr.';
56+
5257
/** @ODM\String */
5358
public $name;
5459

0 commit comments

Comments
 (0)