Skip to content

Commit

Permalink
Modifying DateType to store formatted ISO 8601 date when can't be con…
Browse files Browse the repository at this point in the history
…verted to a unix timestamp for MongoDate creation.
  • Loading branch information
jwage committed Sep 21, 2010
1 parent e6c4bd1 commit b702787
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/Doctrine/ODM/MongoDB/Mapping/Types/DateType.php
Expand Up @@ -35,18 +35,17 @@ public function convertToDatabaseValue($value)
if ($value === null) {
return null;
}
$timestamp = false;
if ($value instanceof \DateTime) {
$timestamp = $value->getTimestamp();
}
if (is_string($value)) {
$timestamp = strtotime($value);
}
// If we have tried to create a timestamp but got false
// then it is likely an invalid timestamp before the unix epoch
// so we can only store the original $value that is just a string
if (isset($timestamp) && $timestamp === false) {
return $value;
// Otherwise set $value to $timestamp to create a MongoDate instance from
// Could not convert date to timestamp so store ISO 8601 formatted date instead
if ($timestamp === false) {
$date = new \DateTime($value);
return $date->format('c');
} else {
$value = $timestamp;
}
Expand Down
1 change: 1 addition & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/DateTest.php
Expand Up @@ -51,6 +51,7 @@ public function testOldDate()
$this->assertTrue(isset($test['createdAt']));

$user = $this->dm->findOne('Documents\User', array('username' => 'datetest2'));
$this->assertTrue($user->getCreatedAt() instanceof \DateTime);
$this->assertEquals('1900-01-01', $user->getCreatedAt()->format('Y-m-d'));
}
}

0 comments on commit b702787

Please sign in to comment.