DDC-3742: Use UTCDatetimeType with lifecycle callbacks #4591

Closed
opened 2026-01-22 14:45:31 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (May 22, 2015).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user mcarra66:

I'm trying to save my entities (yaml), which have lifecyclecallbacks with a createdAt and updatedAt UTCDatetimeType fields. If I save them with prePersist or preUpdate, the insert of new rows into the mysql database fails because of blank date fields, but they are already populated in my entity.

If I replace the UTCDateTimeType by datetime, it works.

Originally created by @doctrinebot on GitHub (May 22, 2015). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user mcarra66: I'm trying to save my entities (yaml), which have lifecyclecallbacks with a createdAt and updatedAt UTCDatetimeType fields. If I save them with prePersist or preUpdate, the insert of new rows into the mysql database fails because of blank date fields, but they are already populated in my entity. If I replace the UTCDateTimeType by datetime, it works.
admin added the BugWon't Fix labels 2026-01-22 14:45:31 +01:00
admin closed this issue 2026-01-22 14:45:31 +01:00
Author
Owner

@doctrinebot commented on GitHub (May 22, 2015):

Comment created by mcarra66:

I'm using UTCDateTimeType from: http://doctrine-orm.readthedocs.org/en/latest/cookbook/working-with-datetime.html

It makes a $value->format($format, $timezone)

I was passing a \Datetime object, so format only has one parameter, not 2...Then, Or I'm passing an unexpected $value or the format method is not properly coded there.

So, I've modified convertToDatabaseValue to the following:

        if ($value === null) {
            return null;
        }

        if (is_null(self::$utc)) {
            self::$utc = new \DateTimeZone('UTC');
        }

        if (!($value instanceof \DateTime)) {
            $value = \DateTime::createFromFormat(
                $platform->getDateTimeFormatString(),
                $value,
                self::$utc
            );
        }

        $value->setTimeZone(self::$utc);

        $val = $value->format($platform->getDateTimeFormatString());

        return $val;
@doctrinebot commented on GitHub (May 22, 2015): Comment created by mcarra66: I'm using `UTCDateTimeType` from: http://doctrine-orm.readthedocs.org/en/latest/cookbook/working-with-datetime.html It makes a `$value->format($format, $timezone)` I was passing a `\Datetime` object, so format only has one parameter, not 2...Then, Or I'm passing an unexpected `$value` or the format method is not properly coded there. So, I've modified `convertToDatabaseValue` to the following: ``` if ($value === null) { return null; } if (is_null(self::$utc)) { self::$utc = new \DateTimeZone('UTC'); } if (!($value instanceof \DateTime)) { $value = \DateTime::createFromFormat( $platform->getDateTimeFormatString(), $value, self::$utc ); } $value->setTimeZone(self::$utc); $val = $value->format($platform->getDateTimeFormatString()); return $val; ```
Author
Owner

@doctrinebot commented on GitHub (May 22, 2015):

Comment created by @ocramius:

[~mcarra66] what exactly is the action to be taken here? It's unclear to me.

@doctrinebot commented on GitHub (May 22, 2015): Comment created by @ocramius: [~mcarra66] what exactly is the action to be taken here? It's unclear to me.
Author
Owner

@doctrinebot commented on GitHub (May 22, 2015):

Comment created by mcarra66:

I replaced the metod convertToDatabaseValue from http://doctrine-orm.readthedocs.org/en/latest/cookbook/working-with-datetime.html with mine.

For example:

date_default_timezone_set('Europe/Madrid'); $expireAt = new \DateTime(); $entity = newEntity(); $entity->setExpireAt($expireAt);

$this->em->persist($entity);
$this->em->flush();

My expireAt field is UTCDateTimeType, so it will be converted from my Europe/Madrid timezone to UTC. But using the code from URL it doesn't works because ->format() method has 2 parameters instead of one.

@doctrinebot commented on GitHub (May 22, 2015): Comment created by mcarra66: I replaced the metod convertToDatabaseValue from http://doctrine-orm.readthedocs.org/en/latest/cookbook/working-with-datetime.html with mine. For example: <code> date_default_timezone_set('Europe/Madrid'); $expireAt = new \DateTime(); $entity = newEntity(); $entity->setExpireAt($expireAt); $this->em->persist($entity); $this->em->flush(); </code> My expireAt field is UTCDateTimeType, so it will be converted from my Europe/Madrid timezone to UTC. But using the code from URL it doesn't works because ->format() method has 2 parameters instead of one.
Author
Owner

@Ocramius commented on GitHub (Aug 23, 2018):

Closing as won't fix: please refer to https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/cookbook/working-with-datetime.html instead

@Ocramius commented on GitHub (Aug 23, 2018): Closing as won't fix: please refer to https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/cookbook/working-with-datetime.html instead
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4591