如何在Doctrine2 SELECT查询中处理DateTime类型的默认值? [英] How to handle default values for a DateTime type in Doctrine2 SELECT query?

查看:132
本文介绍了如何在Doctrine2 SELECT查询中处理DateTime类型的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下Doctrine2实体:

I have the following Doctrine2 entity:

<?php

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
/**
 * @ORM\Entity
 */
class Account
{
    use TimestampableEntity;

    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(type="integer", unique=true, nullable=false)
     */
    private $id;
}

您可以看到我正在使用 Gedmo Timestampable 特质

As you can see I am using Gedmo Timestampable with the Trait provided by the bundle. It's working fine.

问题是当我获取数据并且列 updated_at 出现在时发生的 NULL 0000-00-00 00:00:00 。在这种情况下, DateTime 对象被转换为无效日期,如下所示:

The problem happen when I fetch the data and the column updated_at is coming as NULL 0000-00-00 00:00:00. For such case the DateTime object is translated into an invalid date as shown below:

‌array (
  'RowId' => 26944,
  'synchflag' => 1,
  'updatedAt' => 
  DateTime::__set_state(array(
     'date' => '-0001-11-30 00:00:00.000000',
     'timezone_type' => 3,
     'timezone' => 'UTC',
  )),
)

检查 docs 但没有任何帮助(或者如果您让我知道,至少我没有找到它)

I did check docs but there isn't anything helpful (or at least I didn't found it if you do let me know)

解决这个问题的正确方法是什么?

What is the right way to deal with this?

编辑:

也许正确的问题应该是:如果 updated_at 正以 0000-00-00 00:00:00 的形式出现,如何将其转换为 NOW( )?您可能会注意到 getUpdatedAt()根据获取的数据返回 DateTime SELECT 语句是否有任何事件?

Maybe the right question here should be: if updated_at is coming as 0000-00-00 00:00:00 how do I turn it into NOW()? As you may notice the getUpdatedAt() return a DateTime based on the fetched data. Is there any event for SELECT statement?

推荐答案

数据库已经具有那些无效值,则可以从数据库中读取无效值时创建自己的学说日期时间类型以将转换为null:

As the problem is that the database already have those invalid values, you can create your own doctrine datetime type to make the conversion to null when reading that invalid value from the database:

<?php
namespace AppBundle\Doctrine\DBAL;

use DateTimeZone;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateTimeType;

class MyDateTimeType extends DateTimeType
{


   public function convertToPHPValue($value, AbstractPlatform $platform)
   {
       if (null === $value || $value instanceof \DateTime) {
           return $value;
       }

       if ('0000-00-00 00:00:00' === $value) {
          return null;
       }

       return parent::convertToPHPValue($value, $platform);
   }
}

并在原则配置中注册此自定义类型:

And register this custom type in doctrine configuration:

doctrine:
   dbal:
       types:
           datetime: AppBundle\Doctrine\DBAL\MyDateTimeType

注意:您可以使用regexp和/或datetime有效性检查来增强此代码,而不是简单的比较

Note: you can enhanced this code using regexp and/or datetime validity checking instead of a simple comparison

这篇关于如何在Doctrine2 SELECT查询中处理DateTime类型的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆