Skip to content

Latest commit

 

History

History
80 lines (64 loc) · 2.05 KB

README.md

File metadata and controls

80 lines (64 loc) · 2.05 KB

About

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

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');

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.