2013-07-18 36 views
8

我努力改变它们由使用“artisan migrate”时,如何更改时间戳列名称?

php artisan migrate 

命令生成的时间戳列名。

我已经做了如下修改。当我使用雄辩的查询生成器时,它可以正确生成列名,但是当我使用上面的命令时,它仍然会生成“created_at”,“updated_at”和“deleted_at”。谁能帮我吗?非常感谢。

/* vendor\framework\src\Illuminate\Database\Eloquent\Model.php */ 

/** 
* The name of the "created at" column. 
* 
* @var string 
*/ 
const CREATED_AT = 'datetime_created'; 

/** 
* The name of the "updated at" column. 
* 
* @var string 
*/ 
const UPDATED_AT = 'datetime_updated'; 

/** 
* The name of the "deleted at" column. 
* 
* @var string 
*/ 
const DELETED_AT = 'datetime_deleted'; 

/* vendor\framework\src\Illuminate\Database\Schema\Blueprint.php */ 

/** 
* Indicate that the timestamp columns should be dropped. 
* 
* @return void 
*/ 
public function dropTimestamps() 
{ 
    $this->dropColumn('datetime_created', 'datetime_updated'); 
} 

/** 
* Add a "deleted at" timestamp for the table. 
* 
* @return void 
*/ 
public function softDeletes() 
{ 
    $this->timestamp('datetime_deleted')->nullable(); 
} 
/** 
* Add creation and update timestamps to the table. 
* 
* @return void 
*/ 
public function timestamps() 
{ 
    $this->timestamp('datetime_created'); 

    $this->timestamp('datetime_updated'); 
} 
/** 
* Add a "deleted at" timestamp for the table. 
* 
* @return void 
*/ 
public function softDeletes() 
{ 
    $this->timestamp('datetime_deleted')->nullable(); 
} 

附:我知道修改“核心”并不是一个好主意。如果有人能告诉我扩展这些课程的最佳方式,我将非常感激。

回答

19

千万不要编辑vendor文件夹下的代码。首先是,通常(默认情况下)不会由您的存储库携带,所以如果您或其他人想要在另一台计算机上工作,您将失去更改。 第二个,它会在您执行composer update时被覆盖。


好了,他这样说,让我们开始处理这个“修改的核心是”恐怖的。对于Illuminate\Database\Eloquent\Model.php,只需创建一个基础模型,从中你会延长你的所有后续车型,并覆盖在它的常量:

应用程序/模型/ BaseModel.php

abstract class BaseModel extends Eloquent { 

    /** 
    * The name of the "created at" column. 
    * 
    * @var string 
    */ 
    const CREATED_AT = 'datetime_created'; 

    /** 
    * The name of the "updated at" column. 
    * 
    * @var string 
    */ 
    const UPDATED_AT = 'datetime_updated'; 

    /** 
    * The name of the "deleted at" column. 
    * 
    * @var string 
    */ 
    const DELETED_AT = 'datetime_deleted'; 

} 

然后,对于Illuminate\Database\Schema\Blueprint情况......嗯,它变得血腥:

  1. 扩展..\Schema\Blueprint,覆盖你提到的方法。
  2. 延伸..\Schema\Builder,覆盖createBlueprint方法使用您的新Blueprint类。
    • 此外还扩展..\Schema\MySqlBuilder以扩展您的新Builder类。
  3. 扩展..\Connection,覆盖getSchemaBuilder方法使用您的新Builder类。
    • 也延伸..\MySqlConnection,..\PostgresConnection,..\SqlServerConnection..\SQLiteConnection从您的新Connection类延伸。
    • 注意:..\MySqlConnection还需要有其getSchemaBuilder方法扩展到使用您的新MySqlBuilder类。
  4. 延伸..\ConnectionFactory,覆盖createConnection方法使用您的扩展Connection类。
  5. 创建ServiceProvider将新的ConnectionFactory类注册为新的db.factory组件,并将其添加到app/config/app.php文件的providers下。

所以,半小时通过Laravel的源代码挖掘后找出答案,我得出的结论,这将可能更容易简单,做你的迁移如下:

$table->timestamp(BaseModel::CREATED_AT); 
$table->timestamp(BaseModel::UPDATED_AT); 
+0

大!非常感谢您的回复!我非常感谢你的帮助。自从我在大约8小时前完成我的办公室工作以后,我可能只能在家用电脑上试试看。 – Jonathan

+0

Thakns很多拉斐尔!这工作! – Jonathan