Jira issue originally created by user stof:
Introducing new types converting dates into a DateTimeImmutable rather than a DateTime could be useful for applications prefering the use of immutable datetimes (and relying on PHP 5.5+).
The existing types already support setting a DateTimeImmutable in a field mapped with the datetime type, as it does not check the value against DateTime but only expects a format method. but the conversion from DB to PHP will always produce a mutable DateTime instance, thus preventing to use the immutable flavour consistently.
Not that this is a low priority issue as any code interacting with third party packages will probably need to use DateTime anyway because of the typehints (typehinting DateTimeInterface would not work if you need to keep compatibility with 5.4)
If DBAL 3.0 bumps the minimal supported version to 5.5 (which might be possible depending of the release date of 3.0), we could decide to break BC and use the immutable flavour in the datetime type directly.
Comment created by @deeky666:
[~stof] Should we mark that for 3.0 as suggested? Or do you see any need/possibility of implementing this already in 2.x branch?
Comment created by remper:
Please add this feature. Mutable DateTime causes too much trouble: now we have to use clone in every getter and setter just in case someone modifies the value afterwards and spoils the value stored in entity.
[remper] I don't think we can support this before Doctrine 3.0 without breaking BC as [stof] already stated because it would require us to bump the minimal supported PHP version to 5.5 (which currently is 5.3.2).
Comment created by vasekpurchart:
I've been following this issue for quite long time and I think there are almost no use cases when you want to use DateTime instead of DateTimeImmutable in entities. This can be of course problem with existing code base, but when you start a new project I think you want to go the immutable way. Since I have started several projects recently I've been looking for a temporary solution before 3.0 comes and to my surprise I haven't found any. I had my private solution ready, but now I have opensourced it.
The solution using custom Doctrine DBAL types is quite straightforward so I have implemented these myself: https://github.com/VasekPurchart/Doctrine-Date-Time-Immutable-Types. There is also a bundle ready for Symfony users for an out-of-the box integration. Hope this helps someone. Feedback is appriciated :)