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

x-y*_*uri 4 php database-migration doctrine-orm sql-timestamp laravel-5.2

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

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

但是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.
Run Code Online (Sandbox Code Playgroud)

当我尝试安装mmerian/doctrine-timestampcomposer 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
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

使用composer require mmerian/doctrine-timestamp=dev-master,我能够安装UPD,然后可以安装软件包,然后Type::addType('timestamp', 'DoctrineTimestamp\DBAL\Types\Timestamp');在before 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)
Run Code Online (Sandbox Code Playgroud)

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

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

但这也没有帮助。迁移成功,但列定义未更改。

rsa*_*hez 10

在当前的 Laravel 版本中,您只需将以下内容添加到您的迁移中即可config/database.php更改timestamp列。

use Illuminate\Database\DBAL\TimestampType;
 
'dbal' => [
    'types' => [
        'timestamp' => TimestampType::class,
    ],
],
Run Code Online (Sandbox Code Playgroud)

来源


x-y*_*uri 6

如您所见,mmerian/doctrine-timestamp并不能解决问题。首先,经过这条线 $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)
}
Run Code Online (Sandbox Code Playgroud)

并且$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)
}
Run Code Online (Sandbox Code Playgroud)

所以,首先我ON UPDATE在这里看不到有关零件的信息。其次,唯一的区别是$_type价值。我能确认后,这条线$tableDiff->changedColumns['created_at']->changedProperties

array(1) {
  [0] => string(4) "type"
}
Run Code Online (Sandbox Code Playgroud)

然后,在生成 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;
}
Run Code Online (Sandbox Code Playgroud)

这条线附近的某个地方应该检查Timestamptype 是否'CURRENT_TIMESTAMP'变为CURRENT_TIMESTAMP. 这可能mmerian/doctrine-timestamp吗?这个问题暂时悬而未决。此检查很可能会解决我的特定问题。但现在我要摆脱这个:

DB::statement('ALTER TABLE users MODIFY COLUMN created_at
    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP');
Run Code Online (Sandbox Code Playgroud)


小智 5

您好,您可以使用“ datetime”类型:

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

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

    });
Run Code Online (Sandbox Code Playgroud)

  • 这不允许您使用时间戳功能,例如 useCurrent()。 (3认同)