ursusarctosua/doctrine-timestamp

Realisation of timestamp MySQL type for Doctrine

v0.1.5 2019-11-24 16:29 UTC

This package is auto-updated.

Last update: 2024-05-25 04:00:00 UTC


README

This is MySQL timestamp type implementation for Doctrine ORM.

Using the datetime data type in MySQL may cause a problem when you make a DB connection with different timezones. Instead, the timestamp data type is recommended to make DB server recalculate date values due to timezone.

As far as Doctrine has no type for MySQL timestamp, this lib implements it. It generates timestamp columns when datetimetz column type is specified in entity configuration. Also, to exclude timezone mismatch in DateTime object and MySQL connection, unix timestamp is used to transfer DateTime data from PHP to MySQL and vice versa.

Installation

Use Composer:

composer require ursusarctosua/doctrine-timestamp

Configuration

Doctrine

Register this type with the Doctrine Type system and hook it into the database platform (see Dotrine Custom Mapping Types):

<?php
use Doctrine\DBAL\Types\Type;

Type::addType('datetimetz', 'UrsusArctosUA\DoctrineTimestamp\DBAL\Types\DateTimeTzType');
$conn->getDatabasePlatform()
     ->registerDoctrineTypeMapping('mysql_datetimetz', 'datetimetz');

Symfony

Register type in configuration:

# config/packages/doctrine.yaml

doctrine:
    dbal:
        types:
            datetimetz: UrsusArctosUA\DoctrineTimestamp\DBAL\Types\DateTimeTzType

Usage

Specify this type as a field type in mapping configuration:

<?php
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity()
 */
class MyPersistentClass
{
    /**
    * @var \DateTimeInterface
    * @ORM\Column(type="datetimetz")
    */
    private $field;
}

Known bugs

  • Doctrine schema generator generates requests this do nothing when field marked as not null. However, it works correctly for nullable fields.