Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
<<<'EOF'

// Field(type: "date")
if (isset($data['%1$s'])) {
if (array_key_exists('%1$s', $data) && ($data['%1$s'] !== null || ($this->class->fieldMappings['%2$s']['nullable'] ?? false))) {
$value = $data['%1$s'];
%3$s
$this->class->reflFields['%2$s']->setValue($document, clone $return);
$this->class->reflFields['%2$s']->setValue($document, $return === null ? null : clone $return);
$hydratedData['%2$s'] = $return;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ public function testDateInstanceChangeWhenValueDifferenceIsSubSecond(): void
self::assertNotEmpty($changeset);
}

public function testNullableDateInstanceValue(): void
{
$user = new User();
$user->setCreatedAt(new DateTime('1985-09-01'));
$this->dm->persist($user);
$this->dm->flush();
$this->dm->clear();

$user = $this->dm->getRepository($user::class)->findOneBy([]);
self::assertInstanceOf(DateTime::class, $user->getCreatedAt());
self::assertNull($user->getDisabledAt());
}

public function testDateInstanceValueChangeDoesCauseUpdateIfValueIsTheSame(): void
{
$user = new User();
Expand Down
8 changes: 8 additions & 0 deletions tests/Documents/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class User extends BaseDocument
#[ODM\Field(type: 'date')]
protected $createdAt;

#[ODM\Field(type: 'date', nullable: true, name: 'disable-at')]
protected ?DateTimeInterface $disabledAt;

/** @var Address|null */
#[ODM\EmbedOne(targetDocument: Address::class)]
protected $address;
Expand Down Expand Up @@ -211,6 +214,11 @@ public function getCreatedAt()
return $this->createdAt;
}

public function getDisabledAt(): ?DateTimeInterface
{
return $this->disabledAt;
}

public function getAddress(): ?Address
{
return $this->address;
Expand Down