Permalink
Browse files

Doctrine2 date type can now be used as part of a composite primary key

  • Loading branch information...
1 parent af8e68a commit 028f973242d553e002c7330201a1857034fe2f0e @warezthebeef warezthebeef committed Oct 7, 2011
Showing with 72 additions and 1 deletion.
  1. +2 −1 RealestateMssqlBundle.php
  2. +53 −0 Types/DateType.php
  3. +17 −0 Types/RealestateDateTime.php
@@ -22,7 +22,8 @@ public function boot()
if(!Type::hasType('uniqueidentifier')) {
Type::addType('uniqueidentifier', 'Realestate\MssqlBundle\Types\UniqueidentifierType');
}
-
+
+ Type::overrideType('date', 'Realestate\MssqlBundle\Types\DateType');
Type::overrideType('datetime', 'Realestate\MssqlBundle\Types\DateTimeType');
}
}
View
@@ -0,0 +1,53 @@
+<?php
+/**
+ * Overrides default DateTimeType type to fix the issue with MSSQL's smalldatetime fields
+ *
+ * @author Ken Golovin <ken@webplanet.co.nz>
+ */
+
+namespace Realestate\MssqlBundle\Types;
+
+use Doctrine\DBAL\Platforms\AbstractPlatform;
+use Doctrine\DBAL\Types\Type;
+use Doctrine\DBAL\Types\DateType as BaseDateType;
+use Doctrine\DBAL\Types\ConversionException;
+
+/**
+ * Type that maps an SQL DATE 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());
+ }
+ return $val;
+ }
+
+
+}
@@ -0,0 +1,17 @@
+<?php
+/**
+ *
+ *
+ * @author Ken Golovin <ken@webplanet.co.nz>
+ */
+
+namespace Realestate\MssqlBundle\Types;
+
+class RealestateDateTime extends \DateTime
+{
+ public function __toString()
+ {
+ return $this->format('Y-m-d');
+ }
+}
+

0 comments on commit 028f973

Please sign in to comment.