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

标签 php doctrine-orm database-migration laravel-5.2 sql-timestamp

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

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

  [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');

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

最佳答案

如您所见,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 修改表 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');

关于php - 如何使学说支持时间戳列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34774628/

相关文章:

php - Codeigniter 博客中的文章未更新

php - 如何在多个用户输入后更新同一行?

php - 通过 SQL 查询过滤并填充 <select>

php - 如何使用 Doctrine QueryBuilder 或 EntityManager 通过多对多相关实体查找实体

mysql - Doctrine 2 DQL CONCAT 字段和常量字符串

php - Laravel "partial"播种

php - 确定MySQL中NOW()的时区

php - Symfony 2 与相关实体的验证

php - Laravel 表 * 没有名为 * 的列

node.js - sequelize.sync({ 强制 : true }) is not working some times