Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed fix for date type #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
53 changes: 25 additions & 28 deletions Types/DateType.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* Overrides default DateTimeType type to fix the issue with MSSQL's smalldatetime fields
* Overrides default DateType type to fix the issue with MSSQL's date fields
*
* @author Ken Golovin <[email protected]>
* @author Felix Nagel <[email protected]>
*/

namespace Realestate\MssqlBundle\Types;
Expand All @@ -13,41 +13,38 @@
use Doctrine\DBAL\Types\ConversionException;

/**
* Type that maps an SQL DATE to a PHP DateTime object.
* Type that maps an SQL DATE/TIMESTAMP to a PHP DateTime object.
*
* @since 2.0
*/
class DateType extends BaseDateType
{
public function getName()
{
return Type::DATE;
}

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getDateTypeDeclarationSQL($fieldDeclaration);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return ($value !== null)
? $value->format($platform->getDateFormatString()) : null;
}

public function convertToPHPValue($value, AbstractPlatform $platform)
{
{
if ($value === null) {
return null;
}

$val = \Realestate\MssqlBundle\Types\RealestateDateTime::createFromFormat('!'.$platform->getDateFormatString(), $value);
}

if (!$val) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateFormatString());
if ($value === "") {
return null;
}
return $val;
}

$val = \DateTime::createFromFormat('Y-m-d', $value);
if (!$val) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), 'Y-m-d');
}

}
return $val;
}

/**
*
* @param DateTime $value
* @param AbstractPlatform $platform
* @return string|null
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return ($value !== null) ? $value->format('Y-m-d') : null;
}
}