2014-06-18 28 views

回答

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

如果您有更好的解決方案,請告訴我。

+0

它工作了我! – markdwhite

+1

爲什麼不直接添加表(使用'DB :: statement'調用)?爲什麼添加它,然後改變它? – ajon

+0

@ajon好問題。我這樣做是爲了儘量減少'DB :: statement'的使用。 – Debiprasad

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工作後,因爲我們沒有任何編輯框架代碼。

+0

我在執行此操作時遇到錯誤。 '傳遞給CreateTableName :: {closure}()的參數1必須是ExtendedBlueprint的實例,Illuminate \ Database \ Schema \ Blueprint實例給出'任何想法?我在創建回調中將Blueprint更改爲ExtendedBlueprint。 – gin93r

+0

我想通了。我正在使用'Schema :: create(...)'而不是$ schema-> create(...) – gin93r

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); 
}); 
相關問題