gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 22:10:19 28 4
gpt4 key购买 nike

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

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com