propelorm / Propel2

Propel2 is an open-source high-performance Object-Relational Mapping (ORM) for modern PHP
http://propelorm.org/
MIT License
1.26k stars 399 forks source link

Format insert TIME values as H:i:s.u instead of 'Y-m-d H:i:s.u' #1796

Closed felixgeyer closed 3 years ago

felixgeyer commented 3 years ago

Hi everyone, after changing our timestamp format to Y-m-d\TH:i:s.u\Z we experienced issues with columns of type TIME: Invalid datetime format: 7 ERROR: invalid input syntax for type time: \"2021-10-27T08:00:00.000000Z\"

This happens because there is no dedicated handling of TIME values. In 2.0.0-beta1 DATE values are already handled properly: https://github.com/propelorm/Propel2/commit/0018d43a4480fee50668ac902dae38149252ca24

I suggest to also add this for TIME values like this:

 $script = '';
        if ($column->getType() === PropelTypes::DATE) {
            $columnValueAccessor = $columnValueAccessor . ' ? ' . $columnValueAccessor . '->format("' . $this->getDateFormatter() . '") : null';
        } elseif ($column->getType() === PropelTypes::TIME) {
            $columnValueAccessor = $columnValueAccessor . ' ? ' . $columnValueAccessor . '->format("' . $this->getTimeFormatter() . '") : null';
        } elseif ($column->isTemporalType()) {
            $columnValueAccessor = $columnValueAccessor . ' ? ' . $columnValueAccessor . '->format("' . $this->getTimeStampFormatter() . '") : null';
        } elseif ($column->isLobType()) {
            // we always need to make sure that the stream is rewound, otherwise nothing will
            // get written to database.
            $script .= "
if (is_resource($columnValueAccessor)) {
    rewind($columnValueAccessor);
}";
        }

What do you think? If you agree, I can also make a PR for this change.

dereuromark commented 3 years ago

Go ahead and make a PR It will be easier to review and discuss it.

felixgeyer commented 3 years ago

Sure :-) https://github.com/propelorm/Propel2/pull/1797