From 2dfdd91bb9b7d63694a9dab99c2c1e77ae53b5e7 Mon Sep 17 00:00:00 2001 From: Marek Kilimajer Date: Tue, 31 May 2016 17:59:22 +0200 Subject: [PATCH 1/4] sfDoctrineRecord::getDateTimeObject() returns current time when value is null --- .../lib/record/sfDoctrineRecord.class.php | 19 ++++++++++++++----- .../test/functional/sfDoctrineRecordTest.php | 13 ++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php b/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php index 7418707b0..b20612ad4 100644 --- a/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php +++ b/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php @@ -207,17 +207,22 @@ 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 { @@ -232,11 +237,15 @@ public function getDateTimeObject($dateFieldName) * @param DateTime $dateTimeObject The DateTime instance to use to set the value * @return void */ - 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 (is_null($dateTimeObject)) + { + return $this->set($dateFieldName, null); + } return $this->set($dateFieldName, $dateTimeObject->format('Y-m-d H:i:s')); } else @@ -244,4 +253,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); From 9bd6007b513a2a0d23392f61694bc1955f67fd31 Mon Sep 17 00:00:00 2001 From: Marek Kilimajer Date: Wed, 1 Jun 2016 10:57:16 +0200 Subject: [PATCH 2/4] Use nulll === $ instead of is_null --- .../sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php b/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php index b20612ad4..1b9cfc9c0 100644 --- a/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php +++ b/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php @@ -242,7 +242,7 @@ public function setDateTimeObject($dateFieldName, DateTime $dateTimeObject = nul $type = $this->getTable()->getTypeOf($dateFieldName); if ($type == 'date' || $type == 'timestamp' || $type == 'datetime') { - if (is_null($dateTimeObject)) + if (null === $dateTimeObject) { return $this->set($dateFieldName, null); } From 85b74b025b47af4af8bf7a304fdedb418016ebe1 Mon Sep 17 00:00:00 2001 From: Marek Kilimajer Date: Wed, 1 Jun 2016 12:23:24 +0200 Subject: [PATCH 3/4] Update docblock with @throws --- .../sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php b/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php index 1b9cfc9c0..839204b7b 100644 --- a/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php +++ b/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php @@ -236,6 +236,7 @@ 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 = null) { From 761c2bb8e3a4f61d7dbabacc7b48e903f7af8ee2 Mon Sep 17 00:00:00 2001 From: Marek Kilimajer Date: Fri, 10 Jun 2016 14:17:29 +0200 Subject: [PATCH 4/4] Separate @param and @return --- .../sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php b/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php index 839204b7b..17ba75d14 100644 --- a/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php +++ b/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php @@ -210,6 +210,7 @@ public function __call($method, $arguments) * 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|null $dateTime The instance of PHPs DateTime * @throws sfException if the field is not one of date, datetime, or timestamp types */ @@ -235,6 +236,7 @@ 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 */