2016-01-13 55 views
2

我想申请以下迁移:如何制作学说支持时间戳列?

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

我该怎么办?

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

但它并没有帮助。迁移成功,但列定义不会更改。

+0

快,让你的'composer.json' – maximkou

+0

您可以手动添加https://github.com/mmerian/doctrine-timestamp/blob/ master/lib/DoctrineTimestamp/DBAL/Types/Timestamp.php到您的项目 –

回答

1

人们可以看到,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一部分在这里。其次,onle的区别是$_type的值。我可以证实this line后,$tableDiff->changedColumns['created_at']->changedProperties

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

然后,当generatingALTER TABLEstatement,这一切都归结到这一点

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

设置minimum-stability设置在composer.json 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'); 
+0

降低最低稳定性[不推荐](https://igor.io/2013/02/07/composer-stability-flags.html)。运行'composer安装mmerian/doctrine-timestamp = dev-master'就行。 –

+0

添加第二行没有帮助。 –

0

我建造this它,因为教义并不想支持它,因为它是一个MySQL特有的列类型。

+0

它不适合我。迁移成功,但列定义保持不变:'created_at timestamp NULL DEFAULT NULL'。 –

+0

你还想改变它吗? –

+0

'ALTER TABLE用户MODIFY COLUMN created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP',请参阅我的回答 –

0

嗨〜你可以使用 “日期时间” 类型:

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

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

    });