Skip to content

Cannot save interval type field data #3441

Open
@mattdillon100

Description

@mattdillon100

Bug Report

Q A
BC Break no
Version 2.9.2

Summary

I am using Doctrine with symfony 4.2.2 and trying to persist data to an interval type field in a Postgres database.

Upon saving from the entity, I get an exception from AbstractPostgreSQLDriver saying that the value is invalid.

Current behaviour

It seems that Doctrine is prepending the value a + or -: +P00Y00M00DT01H18M00S.

How to reproduce

  1. Create field and method to be persisted in Symfony entity:

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @var \DateTime
     */
    private $startTime;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @var \DateTime
     */
    private $endTime;

    /**
     * @ORM\Column(type="dateinterval", nullable=true)
     */
    private $duration;

    /**
     * @ORM\PrePersist()
     *
     * @return WorkoutSession
     */
    public function setDuration(): self
    {
        if ($this->endTime) {
            $interval = $this->startTime->diff($this->endTime);
            $interval->invert = 0;

            $this->duration = $interval;
        }

        return $this;
    }
  1. Save an instance of this object.

  2. Doctrine attempts to convert the \DateInterval object ($interval above) to a valid Postgres interval type.

  3. Error is returned:

"An exception occurred while executing 'INSERT INTO workout_sessions (id, start_time, end_time, duration) VALUES (?, ?, ?, ?)' with params [23, \"2019-01-23 08:27:00\", \"2019-01-23 09:45:00\", \"+P00Y00M00DT01H18M00S\"]:\n\nSQLSTATE[22007]: Invalid datetime format: 7 ERROR:  invalid input syntax for type interval: \"+P00Y00M00DT01H18M00S\""

Expected behaviour

Doctrine should only be storing the interval string as a positive number and omitting positive/negative symbols from the beginning of the string.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions