Download the PHP package vasek-purchart/doctrine-date-time-immutable-types without Composer
On this page you can find all versions of the php package vasek-purchart/doctrine-date-time-immutable-types. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vasek-purchart/doctrine-date-time-immutable-types
More information about vasek-purchart/doctrine-date-time-immutable-types
Files in vasek-purchart/doctrine-date-time-immutable-types
Package doctrine-date-time-immutable-types
Short Description Doctrine DateTimeImmutable types
License MIT
Informations about the package doctrine-date-time-immutable-types
Doctrine DBAL DateTimeImmutable Types
In Doctrine DBAL 2.6 immutable DateTime types were added, so if you are using that version or newer, you no longer need this package. If you cannot use that version (it requires PHP 7.1), keep using this package (you can find even PHP 5.6 version in the older versions).
Why would I want to use immutable types?
All Doctrine date/time based types are using DateTime
instances, which are mutable. This can lead to breaking encapsulation and therefore bugs. For two reasons:
1) You accidentally modify a date when you are doing some computation on it:
2) Or you do intentionally try to update it, which fails because Doctrine will not see this:
You can prevent this behaviour by returning a new instance (cloning) or using DateTimeImmutable
(which returns a new instance when modified).
Installation
If you are using Symfony, you can use
vasek-purchart/doctrine-date-time-immutable-types-bundle
, which will take care of the integration.
Install package vasek-purchart/doctrine-date-time-immutable-types
with Composer:
Then you just need to register the types you want:
Or you might want to override some of the default types to work with DateTimeImmutable
instead of DateTime
:
Usage
If you have overridden the default types you don't need to change any mappings.
If you have added the types you only have to suffix your fields with _immutable
: