Skip to content

UrsusArctosUa/doctrine-timestamp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages