Skip to content

Commit 9c3f665

Browse files
marekkj0k3r
authored andcommitted
sfDoctrineRecord::getDateTimeObject() returns current time when value is null (#128)
* sfDoctrineRecord::getDateTimeObject() returns current time when value is null * Use nulll === $ instead of is_null * Update docblock with `@throws` * Separate `@param` and `@return`
1 parent 3b302d9 commit 9c3f665

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,23 @@ public function __call($method, $arguments)
207207
}
208208

209209
/**
210-
* Get the Doctrine date value as a PHP DateTime object
210+
* Get the Doctrine date value as a PHP DateTime object, null if the value is not set
211211
*
212212
* @param string $dateFieldName The field name to get the DateTime object for
213-
* @return DateTime $dateTime The instance of PHPs DateTime
213+
*
214+
* @return DateTime|null $dateTime The instance of PHPs DateTime
215+
* @throws sfException if the field is not one of date, datetime, or timestamp types
214216
*/
215217
public function getDateTimeObject($dateFieldName)
216218
{
217219
$type = $this->getTable()->getTypeOf($dateFieldName);
218220
if ($type == 'date' || $type == 'timestamp' || $type == 'datetime')
219221
{
220-
return new DateTime($this->get($dateFieldName));
222+
$datetime = $this->get($dateFieldName);
223+
if ($datetime)
224+
{
225+
return new DateTime($datetime);
226+
}
221227
}
222228
else
223229
{
@@ -230,18 +236,24 @@ public function getDateTimeObject($dateFieldName)
230236
*
231237
* @param string $dateFieldName The field name to set the date for
232238
* @param DateTime $dateTimeObject The DateTime instance to use to set the value
239+
*
233240
* @return void
241+
* @throws sfException if the field is not one of date, datetime, or timestamp types
234242
*/
235-
public function setDateTimeObject($dateFieldName, DateTime $dateTimeObject)
243+
public function setDateTimeObject($dateFieldName, DateTime $dateTimeObject = null)
236244
{
237245
$type = $this->getTable()->getTypeOf($dateFieldName);
238246
if ($type == 'date' || $type == 'timestamp' || $type == 'datetime')
239247
{
248+
if (null === $dateTimeObject)
249+
{
250+
return $this->set($dateFieldName, null);
251+
}
240252
return $this->set($dateFieldName, $dateTimeObject->format('Y-m-d H:i:s'));
241253
}
242254
else
243255
{
244256
throw new sfException('Cannot call setDateTimeObject() on a field that is not of type date or timestamp.');
245257
}
246258
}
247-
}
259+
}

lib/plugins/sfDoctrinePlugin/test/functional/sfDoctrineRecordTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of the symfony package.
55
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6-
*
6+
*
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
@@ -68,15 +68,18 @@
6868

6969
// Test getDateTimeObject()
7070
$dateTime = $article->getDateTimeObject('created_at');
71-
$t->is($dateTime instanceof DateTime, true);
72-
$t->is($dateTime->format('m/d/Y'), date('m/d/Y'));
71+
$t->is($dateTime, null);
7372

7473
try {
7574
$article->getDateTimeObject('author_id');
7675
$t->fail();
77-
} catch (Exception $e) {
76+
} catch (sfException $e) {
7877
$t->pass();
7978
}
8079

8180
$article->setDateTimeObject('created_at', new DateTime('1985-09-01'));
82-
$t->is($article->getDateTimeObject('created_at')->format('m/d/Y'), '09/01/1985');
81+
$t->is($article->getDateTimeObject('created_at')->format('m/d/Y'), '09/01/1985');
82+
83+
$article->setDateTimeObject('created_at', null);
84+
$dateTime = $article->getDateTimeObject('created_at');
85+
$t->is($dateTime, null);

0 commit comments

Comments
 (0)