沃梦达 / 编程问答 / php问题 / 正文

如何让学说支持时间戳列?

How do I make doctrine support timestamp columns?(如何让学说支持时间戳列?)

本文介绍了如何让学说支持时间戳列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试应用以下迁移:

I'm trying to apply the following migration:

Schema::table('users', function (Blueprint $table) {
    $table->timestamp('created_at')->useCurrent()->change();
});

但是artisan 说:

  [DoctrineDBALDBALException]
  Unknown column type "timestamp" requested. Any Doctrine type that you use has to be registered with DoctrineDBAL
  TypesType::addType(). You can get a list of all the known types with DoctrineDBALTypesType::getTypesMap(). I
  f this error occurs during database introspection then you might have forgot to register all database types for a
  Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMapp
  edDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping inform
  ation.

当我尝试安装 mmerian/doctrine-timestamp (composer install mmerian/doctrine-timestamp) 时,composer 说:

When I try to install mmerian/doctrine-timestamp (composer install mmerian/doctrine-timestamp), composer says:

  [InvalidArgumentException]
  Could not find package mmerian/doctrine-timestamp at any version for your minimum-stability (stable). Check the pa
  ckage spelling or your minimum-stability

我该怎么办?

UPD 使用 composer require mmerian/doctrine-timestamp=dev-master,我能够安装包,然后添加 Type::addType('timestamp', 'DoctrineTimestampDBALTypesTimestamp');Schema::table 语句之前,但现在我有另一个错误:

UPD With composer require mmerian/doctrine-timestamp=dev-master, I was able to install the package, then added Type::addType('timestamp', 'DoctrineTimestampDBALTypesTimestamp'); before Schema::table statement, but now I've got the other error:

  [IlluminateDatabaseQueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'created_at' (SQL: ALTER TABLE u
  sers CHANGE created_at created_at INT DEFAULT 'CURRENT_TIMESTAMP' NOT NULL)

UPD 我再次检查它是否适用于 mmerian/doctrine-timestamp,因为我当时只添加了文档中的第一行(或者文档已更新):

UPD I checked again if it works with mmerian/doctrine-timestamp, since I added only first of the lines from the docs back then (or the doc was updated):

Type::addType('timestamp', 'DoctrineTimestampDBALTypesTimestamp');                                          
DB::getDoctrineConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('Timestamp', 'timestamp');

但这也无济于事.迁移成功,但列定义没有改变.

But it doesn't help as well. The migration succeeds, but the column definition doesn't change.

推荐答案

正如人们所见,mmerian/doctrine-timestamp 并没有解决问题.首先,在这一行之后 $table->getColumns()['created_at']

As one can see, mmerian/doctrine-timestamp doesn't solve the issue. First, after this line $table->getColumns()['created_at'] is

class DoctrineDBALSchemaColumn#520 (16) {
  protected $_type => class DoctrineDBALTypesDateTimeType#504 (0) { }
  protected $_length => NULL
  protected $_precision => int(10)
  protected $_scale => int(0)
  protected $_unsigned => bool(false)
  protected $_fixed => bool(false)
  protected $_notnull => bool(true)
  protected $_default => string(17) "CURRENT_TIMESTAMP"
  protected $_autoincrement => bool(false)
  protected $_platformOptions => array(0) { }
  protected $_columnDefinition => NULL
  protected $_comment => NULL
  protected $_customSchemaOptions => array(0) { }
  protected $_name => string(10) "created_at"
  protected $_namespace => NULL
  protected $_quoted => bool(false)
}

$this->getTableWithColumnChanges($blueprint, $table)->getColumns()['created_at']

class DoctrineDBALSchemaColumn#533 (16) {
  protected $_type => class DoctrineTimestampDBALTypesTimestamp#513 (0) { }
  protected $_length => NULL
  protected $_precision => int(10)
  protected $_scale => int(0)
  protected $_unsigned => bool(false)
  protected $_fixed => bool(false)
  protected $_notnull => bool(true)
  protected $_default => string(17) "CURRENT_TIMESTAMP"
  protected $_autoincrement => bool(false)
  protected $_platformOptions => array(0) { }
  protected $_columnDefinition => NULL
  protected $_comment => NULL
  protected $_customSchemaOptions => array(0) { }
  protected $_name => string(10) "created_at"
  protected $_namespace => NULL
  protected $_quoted => bool(false)
}

所以,首先我在这里看不到有关 ON UPDATE 部分的信息.其次,唯一的区别是 $_type 值.这一行之后我可以确认什么, $tableDiff->changedColumns['created_at']->changedProperties

So, first I can't see information about ON UPDATE part here. Second, the onle difference is $_type value. What I can confirm after this line, $tableDiff->changedColumns['created_at']->changedProperties is

array(1) {
  [0] => string(4) "type"
}

然后,当 生成 ALTER TABLE 声明,归结为这个

Then, when generating ALTER TABLE statement, it all comes down to this

public function getDefaultValueDeclarationSQL($field)
{
    $default = empty($field['notnull']) ? ' DEFAULT NULL' : '';
    if (isset($field['default'])) {
        $default = " DEFAULT '".$field['default']."'";
        if (isset($field['type'])) {
            if (in_array((string) $field['type'], array("Integer", "BigInt", "SmallInt"))) {
                $default = " DEFAULT ".$field['default'];
            } elseif (in_array((string) $field['type'], array('DateTime', 'DateTimeTz')) && $field['default'] == $this->getCurrentTimestampSQL()) {
                $default = " DEFAULT ".$this->getCurrentTimestampSQL();
            } elseif ((string) $field['type'] == 'Time' && $field['default'] == $this->getCurrentTimeSQL()) {
                $default = " DEFAULT ".$this->getCurrentTimeSQL();
            } elseif ((string) $field['type'] == 'Date' && $field['default'] == $this->getCurrentDateSQL()) {
                $default = " DEFAULT ".$this->getCurrentDateSQL();
            } elseif ((string) $field['type'] == 'Boolean') {
                $default = " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
            }
        }
    }
    return $default;
}

这一行应该是检查Timestamp类型以将'CURRENT_TIMESTAMP'转换为CURRENT_TIMESTAMP.这在 mmerian/doctrine-timestamp 中可能吗?这个问题暂时悬而未决.此检查很可能会解决我的特定问题.但现在我要摆脱这个:

Somewhere around this line there supposed to be a check for Timestamp type to turn 'CURRENT_TIMESTAMP' into CURRENT_TIMESTAMP. Is this possible within mmerian/doctrine-timestamp? That question is left open for now. This check would most likely solve my particular issue. But for now I'm going to get away with this:

DB::statement('ALTER TABLE users MODIFY COLUMN created_at
    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP');

这篇关于如何让学说支持时间戳列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何让学说支持时间戳列?

基础教程推荐