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

How do I make doctrine support timestamp columns?

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

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

但是 artisan 说:

  [Doctrine\DBAL\DBALException]
  Unknown column type "timestamp" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL
  \Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::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 说:

  [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,我能够安装软件包,然后在 Schema::table 语句之前添加 Type::addType('timestamp', 'DoctrineTimestamp\DBAL\Types\Timestamp');,但现在我'我遇到了另一个错误:

  [Illuminate\Database\QueryException]
  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,因为当时我只添加了文档中的第一行(或者文档已更新):

Type::addType('timestamp', 'DoctrineTimestamp\DBAL\Types\Timestamp');                                          
DB::getDoctrineConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('Timestamp', 'timestamp');

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

在你的composer.json中将minimum-stability设置为dev,因为mmerian/doctrine-timestamp只有dev-master版本,例如:

{
    "minimum-stability": "dev",
    "require": {
        ...
     }
}

Then, when bootstraping your doctrine connection:

Type::addType('timestamp', 'DoctrineTimestamp\DBAL\Types\Timestamp');
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('Timestamp', 'timestamp');

如您所见,mmerian/doctrine-timestamp 没有解决问题。首先,this line之后$table->getColumns()['created_at']

class Doctrine\DBAL\Schema\Column#520 (16) {
  protected $_type => class Doctrine\DBAL\Types\DateTimeType#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 Doctrine\DBAL\Schema\Column#533 (16) {
  protected $_type => class DoctrineTimestamp\DBAL\Types\Timestamp#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 值。 this line$tableDiff->changedColumns['created_at']->changedProperties后我可以确认的是

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

然后,当generating ALTER TABLE statement,一切归结为

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;
}

this line 附近的某处应该检查 Timestamp 类型以将 'CURRENT_TIMESTAMP' 转换为 CURRENT_TIMESTAMP。这在 mmerian/doctrine-timestamp 内可能吗?这个问题暂时悬而未决。此检查很可能会解决我的特定问题。但现在我要摆脱这个:

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

我为它构建了 this,因为 Doctrine 不想支持它,因为它是 MySQL 特定的列类型。

嗨~你可以使用"datetime"类型:

 Schema::table('orders', function ($table) {

        $table->datetime('pay_time')->nullable()->change();

    });

如果你想为当前时间戳进行迁移并得到错误“请求的未知列类型”timestamp“。你使用的任何 Doctrine 类型都必须在 \Doctrine\DBAL\Types\Type::addType() 中注册” 然后像这样使用

\DB::statement("ALTER TABLE `order_status_logs` CHANGE `created_at` `created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP");