Commit a1e4c86b authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-33 - Added flexible DateTime type that can be used as replacement when...

DBAL-33 - Added flexible DateTime type that can be used as replacement when variable date/time parts are returned from the database, for example with PostgreSQL TIMESTAMP(n) and TIME(n) types
parent 67947934
......@@ -559,7 +559,7 @@ class PostgreSqlPlatform extends AbstractPlatform
*/
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
{
return 'TIME';
return 'TIME(0) WITHOUT TIME ZONE';
}
/**
......
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Variable DateTime Type using date_create() instead of DateTime::createFromFormat()
*
* This type has performance implications as it runs twice as long as the regular
* {@see DateTimeType}, however in certain PostgreSQL configurations with
* TIMESTAMP(n) columns where n > 0 it is necessary to use this type.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 2.0
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class VarDateTimeType extends DateTimeType
{
/**
* @throws ConversionException
* @param string $value
* @param AbstractPlatform $platform
* @return DateTime
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}
$val = date_create($value);
if (!$val) {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val;
}
}
\ No newline at end of file
......@@ -35,6 +35,7 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\ObjectTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\DateTimeTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\DateTimeTzTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\VarDateTimeTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\DateTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\TimeTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\BooleanTest');
......
<?php
namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks;
require_once __DIR__ . '/../../TestInit.php';
class VarDateTimeTest extends \Doctrine\Tests\DbalTestCase
{
protected
$_platform,
$_type;
protected function setUp()
{
$this->_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
if (!Type::hasType('vardatetime')) {
Type::addType('vardatetime', 'Doctrine\DBAL\Types\VarDateTimeType');
}
$this->_type = Type::getType('vardatetime');
}
public function testDateTimeConvertsToDatabaseValue()
{
$date = new \DateTime('1985-09-01 10:10:10');
$expected = $date->format($this->_platform->getDateTimeTzFormatString());
$actual = is_string($this->_type->convertToDatabaseValue($date, $this->_platform));
$this->assertEquals($expected, $actual);
}
public function testDateTimeConvertsToPHPValue()
{
// Birthday of jwage and also birthday of Doctrine. Send him a present ;)
$date = $this->_type->convertToPHPValue('1985-09-01 00:00:00', $this->_platform);
$this->assertType('DateTime', $date);
$this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
$this->assertEquals('000000', $date->format('u'));
}
public function testInvalidDateTimeFormatConversion()
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform);
}
public function testConversionWithMicroseconds()
{
$date = $this->_type->convertToPHPValue('1985-09-01 00:00:00.123456', $this->_platform);
$this->assertType('DateTime', $date);
$this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
$this->assertEquals('123456', $date->format('u'));
}
public function testNullConversion()
{
$this->assertNull($this->_type->convertToPHPValue(null, $this->_platform));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment