2017-05-29 4 views
0

私はYii2フレームワークで非常に新しいです。私はこれを学び始めます... 今、私はFKで2つのテーブルを作ろうとしますが、私はそれを理解できません。誰かが私に言うことができるなら私は幸せになるでしょう。fiiでのYii2の移行

移行1:

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

私は2番目のマイグレーションで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あなたはそんなに! –

関連する問題