2014-06-18 15 views

Odpowiedz

9

Jak teraz laravel Schema Builder nie obsługuje SET typ danych dla kolumny. Oto alternatywne rozwiązanie, dopóki ktoś nie doda tych kodów do Laravel.

Krok 1: Utwórz tabelę, użyj ENUM zamiast 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(); 
}); 

Teraz zmień ENUM na SET.

$table_prefix = DB::getTablePrefix(); 
DB::statement("ALTER TABLE `" . $table_prefix . "schools` CHANGE `level` `level` SET('Preschool','Kindergarten','Primary','Secondary');"); 

Jeśli masz lepsze rozwiązanie, proszę dać mi znać.

+0

pracował dla mnie! – markdwhite

+1

Dlaczego nie po prostu dodać tabeli później (za pomocą wywołania 'DB :: instrukcja")? Dlaczego ją dodajesz, a następnie zmieniasz? – ajon

+0

@ajon Dobre pytanie. Uczyniłem tak, aby ograniczyć użycie 'DB :: statement' do minimum. – Debiprasad

11

Krok 1. Extend klas domyślnych (dodaj ten kod do pliku migracji po use sekcje):

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

} 

Krok 2. Następnie musimy zmienić domyślne klasy gramatyczne i plan na nasze zwyczaj:

// 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); 
}); 

Ta metoda działa również po composer update, bo nie edytować dowolny kod ramowej.

+0

Otrzymuję błąd, gdy to robię. 'Argument 1 przekazany do CreateTableName :: {closure}() musi być instancją ExtendedBlueprint, instancją Illuminate \ Database \ Schema \ Blueprint given' Wszelkie myśli? Zmieniłem Blueprint na ExtendedBlueprint w moim wywołaniu oddzwonienia. – gin93r

+0

Rozgryzłem to. Używałem 'Schema :: create (...)' zamiast $ schema-> create (...) – gin93r

4

metoda Roman Nazarkin za działa niemal idealnie jednak jest mały problem z prefiksów tabeli (których metoda ta nie uwzględnia) to jest proste jednak do tej pracy sugestia z prefiksów tabeli:

$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); 
}); 
Powiązane problemy