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
22 changes: 17 additions & 5 deletions lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an empty line before this @return tag? (Like this)

* @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
{
Expand All @@ -230,18 +236,24 @@ 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
{
throw new sfException('Cannot call setDateTimeObject() on a field that is not of type date or timestamp.');
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
*
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand Down Expand Up @@ -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');
$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);