2017-05-29 92 views
0

我是Yii2框架中非常新的。我開始學習這個...... 現在我嘗試用FK製作2張桌子,但我無法理解它。我會很高興,如果任何人都可以說我必須與FK一起看桌子。Yii2遷移與fk

遷移之一:

public function up() 
{ 
    $this->createTable('portfolio', [ 
     'id' => $this->primaryKey(), 
     'project_name'   => $this->string()->notNull(), 
     'main_image'   => $this->string(), 
     'galery'    => $this->string(), 
     'link_to_live_project' => $this->string()->notNull(), 
     'short_description'  => $this->string(), 
     'full_description'  => $this->string()->notNull(), 
     'date_released'   => $this->string(), 
     'technologies'   => $this->string()->notNull(), 
     'created_at'   => $this->dateTime(), 
    ]); 
} 

/** 
* @inheritdoc 
*/ 
public function down() 
{ 
    $this->dropTable('portfolio'); 
} 

二遷移:

public function up() 
{ 
    $this->createTable('gallery_to_portfolio', [ 
     'id' => $this->primaryKey(), 
    ]); 
} 

/** 
* @inheritdoc 
*/ 
public function down() 
{ 
    $this->dropTable('gallery_to_portfolio'); 
} 

我想讓FK在第二個遷移。

回答

1

您的第二個遷移了函數的代碼應該看起來像以下:

$this->createTable('gallery_to_portfolio', [ 
     'id' => $this->primaryKey(), 
     'portfolioId' => $this->integer()->notNull(), 
     ... other fields ... 
    ]); 

$this->addForeignKey('fk-gallery_to_portfolio-portfolio','gallery_to_portfolio','portfolioId','portfolio','id','cascade'); 
+0

TNX你這麼多,先生! –