Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-30

DateTimeType should take into account null as a possible value

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.0-ALPHA2
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      Description

      DateTimeType does not expect the value to be null, both conversions ignore this possibility. This bug is especially annoying when converting from the database to PHP. The user does not expect to get false as a value for a datetime field. False is returned by DateTime when null is given. Null should be returned as the PHP value. Converting to the database from PHP would result in an error, since $value would be null.

      Simple fix:

      OLD:

      public function convertToDatabaseValue($value, AbstractPlatform $platform)
          {
      	return $value->format($platform->getDateTimeFormatString());
          }
          
          public function convertToPHPValue($value, AbstractPlatform $platform)
          {
      	return \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value);
          }
      

      NEW:

      public function convertToDatabaseValue($value, AbstractPlatform $platform)
          {
      		if ($value !== null)
      		{
      			return $value->format($platform->getDateTimeFormatString());
      		}
      		else
      		{
      			return null;
      		}
          }
          
          public function convertToPHPValue($value, AbstractPlatform $platform)
          {
      		if ($value !== null)
      		{
      			return \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value);
      		}
      		else
      		{
      			return null;
      		}
          }
      

        Activity

        Hide
        Ismo Toijala added a comment -

        The same problem exists in DateType an TimeType as well. The same kind of fix should work.

        Show
        Ismo Toijala added a comment - The same problem exists in DateType an TimeType as well. The same kind of fix should work.
        Hide
        Guilherme Blanco added a comment -

        In changeset http://trac.doctrine-project.org/changeset/6418 I committed a fix to this report!

        Thanks for report and patch!

        Show
        Guilherme Blanco added a comment - In changeset http://trac.doctrine-project.org/changeset/6418 I committed a fix to this report! Thanks for report and patch!

          People

          • Assignee:
            Guilherme Blanco
            Reporter:
            Ismo Toijala
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: