Skip to content

Commit

Permalink
Merge pull request #1466 from malarzm/hydrating-nullable
Browse files Browse the repository at this point in the history
Fix hydrating null value when field is nullable
  • Loading branch information
malarzm committed Jul 27, 2016
2 parents 4ac50ee + f80a89d commit 06c4cad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php
Expand Up @@ -232,9 +232,13 @@ private function generateHydratorClass(ClassMetadata $class, $hydratorClassName,
$code .= sprintf(<<<EOF
/** @Field(type="{$mapping['type']}") */
if (isset(\$data['%1\$s'])) {
if (isset(\$data['%1\$s']) || (! empty(\$this->class->fieldMappings['%2\$s']['nullable']) && array_key_exists('%1\$s', \$data))) {
\$value = \$data['%1\$s'];
%3\$s
if (\$value !== null) {
%3\$s
} else {
\$return = null;
}
\$this->class->reflFields['%2\$s']->setValue(\$document, \$return);
\$hydratedData['%2\$s'] = \$return;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/HydratorTest.php
Expand Up @@ -13,6 +13,7 @@ public function testHydrator()
$user = new HydrationClosureUser();
$this->dm->getHydratorFactory()->hydrate($user, array(
'_id' => 1,
'title' => null,
'name' => 'jon',
'birthdate' => new \DateTime('1961-01-01'),
'referenceOne' => array('$id' => '1'),
Expand All @@ -31,6 +32,7 @@ public function testHydrator()
));

$this->assertEquals(1, $user->id);
$this->assertSame(null, $user->title);
$this->assertEquals('jon', $user->name);
$this->assertInstanceOf('DateTime', $user->birthdate);
$this->assertInstanceOf(__NAMESPACE__.'\HydrationClosureReferenceOne', $user->referenceOne);
Expand All @@ -49,6 +51,9 @@ class HydrationClosureUser
/** @ODM\Id */
public $id;

/** @ODM\Field(type="string", nullable=true) */
public $title = 'Mr.';

/** @ODM\String */
public $name;

Expand Down

0 comments on commit 06c4cad

Please sign in to comment.