soomedia/doctrine-utcdatetime

始终将你的 DateTime 存储在 UTC 时区的 Doctrine2 DateTimeType

dev-master 2016-03-30 11:52 UTC

This package is auto-updated.

Last update: 2024-08-28 03:43:04 UTC


README

始终将你的 DateTime 存储在 UTC 时区的 Doctrine2 DateTimeType。

此库中所有的代码都来源于 DoctrineExtensions 的文档。我并不为此代码拥有任何版权,我只是简单地将它打包成一个易于使用的包。

使用 UTCDateTimeType

添加库

使用 Composer 将库添加到你的项目中

$ composer require soomedia/doctrine-utcdatetime

覆盖默认的日期时间类型

直接从 DoctrineExtensions 的文档复制

我们只需注册并覆盖 datetime 类型。警告:这将会覆盖所有实体以及外部包或扩展中的 datetime 类型,所以如果你的某些实体需要 Doctrine 的标准 datetime 类型,你必须修改上述类型并使用不同的名称(例如 utcdatetime)。此外,如果你使用 DoctrineExtensions,你需要修改 Timestampable 以包括 utcdatetime 作为有效的类型。

在 Symfony2 中你可以这样做

doctrine:
    dbal:
        types:
            datetime: SooMedia\Doctrine\DBAL\Types\UTCDateTimeType

在 Zend Framework 2 中,你需要在你的 module.config.php 中添加以下内容

<?php
return array(
    // other module config here
    'doctrine' => array(
        'configuration' => array(
            'orm_default' => array( // use the name of your default configuration/connection
                'types' => array(
                    'datetime' => 'SooMedia\Doctrine\DBAL\Types\UTCDateTimeType',
                ),
            ),
        ),
        // other configuration here, like the driver config
    ),
);

致谢

正如我所说的:这段出色的代码的版权完全归 DoctrineExtensions 所有。DoctrineExtensions 为 Doctrine 添加了大量的额外功能,所以我真心推荐使用它!