diff --git a/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php b/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php index 7418707b0..17ba75d14 100644 --- a/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php +++ b/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php @@ -207,17 +207,23 @@ public function __call($method, $arguments) } /** - * Get the Doctrine date value as a PHP DateTime object + * Get the Doctrine date value as a PHP DateTime object, null if the value is not set * * @param string $dateFieldName The field name to get the DateTime object for - * @return DateTime $dateTime The instance of PHPs DateTime + * + * @return DateTime|null $dateTime The instance of PHPs DateTime + * @throws sfException if the field is not one of date, datetime, or timestamp types */ public function getDateTimeObject($dateFieldName) { $type = $this->getTable()->getTypeOf($dateFieldName); if ($type == 'date' || $type == 'timestamp' || $type == 'datetime') { - return new DateTime($this->get($dateFieldName)); + $datetime = $this->get($dateFieldName); + if ($datetime) + { + return new DateTime($datetime); + } } else { @@ -230,13 +236,19 @@ public function getDateTimeObject($dateFieldName) * * @param string $dateFieldName The field name to set the date for * @param DateTime $dateTimeObject The DateTime instance to use to set the value + * * @return void + * @throws sfException if the field is not one of date, datetime, or timestamp types */ - public function setDateTimeObject($dateFieldName, DateTime $dateTimeObject) + public function setDateTimeObject($dateFieldName, DateTime $dateTimeObject = null) { $type = $this->getTable()->getTypeOf($dateFieldName); if ($type == 'date' || $type == 'timestamp' || $type == 'datetime') { + if (null === $dateTimeObject) + { + return $this->set($dateFieldName, null); + } return $this->set($dateFieldName, $dateTimeObject->format('Y-m-d H:i:s')); } else @@ -244,4 +256,4 @@ public function setDateTimeObject($dateFieldName, DateTime $dateTimeObject) throw new sfException('Cannot call setDateTimeObject() on a field that is not of type date or timestamp.'); } } -} \ No newline at end of file +} diff --git a/lib/plugins/sfDoctrinePlugin/test/functional/sfDoctrineRecordTest.php b/lib/plugins/sfDoctrinePlugin/test/functional/sfDoctrineRecordTest.php index 68ec06b6e..95273cd39 100644 --- a/lib/plugins/sfDoctrinePlugin/test/functional/sfDoctrineRecordTest.php +++ b/lib/plugins/sfDoctrinePlugin/test/functional/sfDoctrineRecordTest.php @@ -3,7 +3,7 @@ /* * This file is part of the symfony package. * (c) 2004-2006 Fabien Potencier - * + * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -68,15 +68,18 @@ // Test getDateTimeObject() $dateTime = $article->getDateTimeObject('created_at'); -$t->is($dateTime instanceof DateTime, true); -$t->is($dateTime->format('m/d/Y'), date('m/d/Y')); +$t->is($dateTime, null); try { $article->getDateTimeObject('author_id'); $t->fail(); -} catch (Exception $e) { +} catch (sfException $e) { $t->pass(); } $article->setDateTimeObject('created_at', new DateTime('1985-09-01')); -$t->is($article->getDateTimeObject('created_at')->format('m/d/Y'), '09/01/1985'); \ No newline at end of file +$t->is($article->getDateTimeObject('created_at')->format('m/d/Y'), '09/01/1985'); + +$article->setDateTimeObject('created_at', null); +$dateTime = $article->getDateTimeObject('created_at'); +$t->is($dateTime, null);