Skip to content

Loading…

DDC-30: DateTimeType should take into account null as a possible value #3765

Closed
doctrinebot opened this Issue · 3 comments

2 participants

@doctrinebot

Jira issue originally created by user itoijala:

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;
        }
    }
@doctrinebot

Comment created by itoijala:

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

@doctrinebot

Comment created by @guilhermeblanco:

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

Thanks for report and patch!

@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot doctrinebot added this to the 2.0-ALPHA2 milestone
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.