Laravel Schema有一個等同於該表的ENUM命令。什麼是SET相當於表?在Laravel架構中,什麼是MySQL數據類型SET相當於?
5
A
回答
9
截至目前Laravel架構生成器不支持SET數據類型的列。所以,在有人將這些代碼添加到Laravel之前,這裏有一個替代解決方案。
第1步:創建表,使用ENUM而不是SET。
Schema::create('schools', function($table)
{
$table->increments('id');
$table->char('id_number', 6);
$table->string('school_name');
$table->enum('level', array('Preschool', 'Kindergarten', 'Primary', 'Secondary'))->index(); // *** fix this
$table->string('phone');
$table->string('email');
$table->string('location');
$table->smallInteger('city')->unsigned()->index();
$table->smallInteger('country')->unsigned()->index();
$table->smallInteger('head_teacher')->unsigned()->index();
$table->smallInteger('director')->unsigned()->index();
$table->smallInteger('created_by')->unsigned();
$table->smallInteger('modified_by')->unsigned();
$table->timestamps();
});
現在將ENUM更改爲SET。
$table_prefix = DB::getTablePrefix();
DB::statement("ALTER TABLE `" . $table_prefix . "schools` CHANGE `level` `level` SET('Preschool','Kindergarten','Primary','Secondary');");
如果您有更好的解決方案,請告訴我。
-1
根據Laravel API,我認爲不可能使用Schema Builder創建一個集合。
來源:http://laravel.com/api/class-Illuminate.Database.Schema.Blueprint.html
11
步驟1.擴展缺省類(此代碼後use
部分添加到您的遷移文件):
class ExtendedBlueprint extends Blueprint {
/**
* Create a new set column on the table.
*
* @param string $column
* @param array $allowed
* @return \Illuminate\Support\Fluent
*/
public function set($column, array $allowed)
{
return $this->addColumn('set', $column, compact('allowed'));
}
}
class ExtendedMySqlGrammar extends Illuminate\Database\Schema\Grammars\MySqlGrammar {
/**
* Create the column definition for an set type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeSet(\Illuminate\Support\Fluent $column)
{
return "set('".implode("', '", $column->allowed)."')";
}
}
第2步:然後,我們需要改變默認的語法和藍圖類我們定製:
// set new grammar class
DB::connection()->setSchemaGrammar(new ExtendedMySqlGrammar());
// get custom schema object
$schema = DB::connection()->getSchemaBuilder();
// bind new blueprint class
$schema->blueprintResolver(function($table, $callback) {
return new ExtendedBlueprint($table, $callback);
});
// then create tables
$schema->create('table name', function(ExtendedBlueprint $table)
{
$table->increments('id');
$table->text('sentence');
$table->string('author')->nullable();
$table->string('source')->nullable();
$table->set('difficulty', range(1, 10)); // use our new mysql type
$table->boolean('enabled')->default(true);
});
這種方法也將composer update
工作後,因爲我們沒有任何編輯框架代碼。
4
羅馬Nazarkin的方法效果近乎完美但有一個小問題與表前綴(此方法不考慮),它是簡單不過,以與表前綴這個建議的工作:
$grammar = DB::connection()->withTablePrefix(new ExtendedMySqlGrammar());
// set new grammar class
DB::connection()->setSchemaGrammar($grammar);
// get custom schema object
$schema = DB::connection()->getSchemaBuilder();
// bind new blueprint class
$schema->blueprintResolver(function($table, $callback) {
return new ExtendedBlueprint($table, $callback);
});
// then create tables
$schema->create('table name', function(ExtendedBlueprint $table)
{
$table->increments('id');
$table->text('sentence');
$table->string('author')->nullable();
$table->string('source')->nullable();
$table->set('difficulty', range(1, 10)); // use our new mysql type
$table->boolean('enabled')->default(true);
});
相關問題
- 1. 在MySQL中使用`set`數據類型
- 2. gvim的「set macligatures」(macvim)相當於什麼?
- 3. 什麼是適用於HIVE的nvarchar,MYSQL數據庫的適當數據類型?
- 4. C# - 「set」訪問器中「value」的數據類型是什麼?
- 5. 什麼是REBOL結構!數據類型?
- 6. unix_timestamp(MySQL)的數據類型是什麼?
- 7. 什麼是.Net類型相當於悖論數據庫中的時間
- 8. 什麼是$數組[] = $價值在laravel集合相當於
- 9. 什麼是DBMS_OUTPUT的MySQL相當於
- 10. 相當於MySQL數據和時間數據類型的C#
- 11. 代數數據類型相當於C
- 12. 什麼是類型和什麼是類型構造函數scala
- 13. 什麼是在PHP中相當於top.location?
- 14. DrRacket中是否有相當於set-car!和set-cdr!在SICP?
- 15. 外鍵與SET數據類型MySQL
- 16. MYSQL |將值插入SET數據類型
- 17. C#相當於C++/CLI數據類型
- 18. 什麼是BindingFlags.Default相當於?
- 19. 什麼是在MySQL中存儲布爾值的數據類型?
- 20. 什麼是相當於MySQL函數的Doctrine 2 DQL函數st_within
- 21. 相當於void(MainWindow :: * handler)()的std :: mem_fn類型是什麼?
- 22. 什麼是C#相當於Haskell的新類型?
- 23. 相當於MySQL枚舉數據類型的SQL Server?
- 24. MySQL中SET類型的用法是什麼?如何在Oracle中實現?
- 25. 什麼是相應的C++數據類型爲SQL數字(18,0)數據類型?
- 26. 爲什麼Haskell中沒有內置的Set數據類型?
- 27. 什麼是mysql中的DC2Type數組數據類型
- 28. 什麼類型的數據用於在mysql中保存數據時間
- 29. Python數據類型,它是什麼樣的數據結構?
- 30. 什麼是數據結構添加不同類型的數據?
它工作了我! – markdwhite
爲什麼不直接添加表(使用'DB :: statement'調用)?爲什麼添加它,然後改變它? – ajon
@ajon好問題。我這樣做是爲了儘量減少'DB :: statement'的使用。 – Debiprasad