doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.86k stars 2.5k forks source link

Entity with "date_immutable" field returned from database has DateTime instead of DateTimeImmutable #8093

Open HarasimowiczKamil opened 4 years ago

HarasimowiczKamil commented 4 years ago

Bug Report

Q A
BC Break no
Version 2.7.1

Summary

Entity with "date_immutable" field returned from database has DateTime instead of DateTimeImmutable. If I change the annotation to "datetime_immutable" it works fine. I use Mysql engine if it matters.

Current behavior

How to reproduce

declare(strict_types=1);

namespace App\Entity;

class Test
{
    /**
     * @var int
     *
     * @ORM\Column(type="integer", nullable=false, options={"unsigned": true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \DateTimeImmutable
     *
     * @ORM\Column(name="`from`", type="date_immutable")
     */
    private $from;

    public function getFrom(): \DateTimeImmutable
    {
        return $this->from;
    }
}
$this->repository = $entityManager->getRepository(Test::class)->find(1)->getFrom();

this throw exception:

Return value of App\Entity\Test::getFrom() must be an instance of DateTimeImmutable, instance of DateTime returned

Expected behavior

The expected behavior is that it would be a DateTimeImmutable object :smile:

akovalyova commented 1 year ago

Have similar error - with Repository->findOneByField (createQueryBuilder) returning the values from DB.

Cannot assign DateTime to property App\Entity\Post::$createdAt of type ?DateTimeImmutable

Have to make field to be DateTime in order to work normally with entity. :( Really unexpected behavior, considering that it DateTimeImmutable was generated by make:entity so should work by default.

gimler commented 1 month ago

@greg0ire old issue but still exist. I run into the same problem today. Can you take a look at it, please?

GooseBumpsOS commented 1 month ago

@greg0ire old issue but still exist. I run into the same problem today. Can you take a look at it, please?

try

    #[ORM\Column(name: 'created_at', type: 'datetime_immutable' .....
greg0ire commented 1 month ago

@gimler can you reproduce the issue with a unit test?