mapado / pretty-types
Doctrine Type storing pretty printed JSON into database
Requires
- php: ^8.4
- doctrine/dbal: ^4.4
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.0
Suggests
None
Provides
None
Conflicts
- doctrine/orm: <3.6
Replaces
None
README
Installation
composer require mapado/pretty-types
Compatible with doctrine/dbal 4.4+, and doctrine/orm 3.6+ if you use it.
JSON pretty
Based on Doctrine DBAL types.
Will store pretty printed JSON into database.
Configuration
In your config file
# Doctrine Configuration doctrine: dbal: types: json_pretty: Mapado\PrettyTypes\JsonPrettyType
Usage
In your entities
/** * ... * * @ORM\Column(name="column_name", type="json_pretty") */
UTCDateTime
If you want to store datetime in UTC in your database.
Copied version from Working with DateTime Instances.
All credits goes to the doctrine team !
Configuration
In your config file
# Doctrine Configuration doctrine: dbal: types: datetime: Mapado\PrettyTypes\UTCDateTimeType datetime_immutable: Mapado\PrettyTypes\UTCDateTimeImmutableType datetimetz: Mapado\PrettyTypes\UTCDateTimeType
Be aware that this will override all datetimes configured with doctrine.
If you do not want to override everything and use a custom types instead:
# Doctrine Configuration doctrine: dbal: types: utcdatetime: Mapado\PrettyTypes\UTCDateTimeType utc_datetime_immutable: Mapado\PrettyTypes\UTCDateTimeImmutableType
Usage
In your entities, if you overrode the default datetime types, Doctrine will implicitly use the new types for all datetime fields:
#[ORM\Column] private \DateTime $someDate; #[ORM\Column] private \DateTimeImmutable $someDate;
UTCDateTimeType accepts both \DateTime and \DateTimeImmutable values when writing to the database.
It adapts UTCDateTimeImmutableType instead of extending the DBAL DateTimeType, deprecated since doctrine/dbal 4.6.
You should store the timezone next to the datetime too. Read the doctrine documentation for more informations.
If you configured a custom type, use this instead:
#[ORM\Column(type: 'utcdatetime')] private \DateTime $someDate; #[ORM\Column(type: 'utc_datetime_immutable')] private \DateTimeImmutable $someDate;
Mutable date
doctrine/dbal 4.6 deprecates its mutable DateType.
MutableDateType keeps \DateTime properties on date columns without that deprecation:
it adapts DateImmutableType, as suggested in the DBAL upgrade notes.
Configuration
In your config file
# Doctrine Configuration doctrine: dbal: types: date: Mapado\PrettyTypes\MutableDateType
Usage
In your entities
#[ORM\Column(type: 'date')] private \DateTime $someDate;
MutableDateType accepts both \DateTime and \DateTimeImmutable values when writing to the database.
Legacy array
doctrine/dbal 4 dropped the built-in array type (PHP serialize() stored in a text column).
LegacyArrayType is a port of the DBAL 3 ArrayType, so an application upgrading to DBAL 4 keeps reading and writing its existing array columns, with one restriction on objects (see below).
Configuration
In your config file
# Doctrine Configuration doctrine: dbal: types: array: Mapado\PrettyTypes\LegacyArrayType
Usage
In your entities
#[ORM\Column(type: 'array')] private array $someArray;
Prefer the json type for new columns: this type only exists to keep existing data readable.
Unlike the DBAL 3 ArrayType, it only unserializes DateTime and DateTimeImmutable objects (enum cases are restored too).
Any other object stored in the column comes back as __PHP_Incomplete_Class.
Dates are allowed because overriding array also covers third-party mappings, such as Gedmo Loggable's AbstractLogEntry::$data, which stores them in its changesets.
License
This project is licensed under the MIT license.