Reputation: 141
i`m very new in Yii2 framework. I start to learn this ... Now i try to make 2 tables with FK but i cant understand it. I will happy if anyone can say me how must look the table with FK.
Migration one:
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');
}
Second migration:
public function up()
{
$this->createTable('gallery_to_portfolio', [
'id' => $this->primaryKey(),
]);
}
/**
* @inheritdoc
*/
public function down()
{
$this->dropTable('gallery_to_portfolio');
}
I wanna make fk in second migration.
Upvotes: 1
Views: 65
Reputation: 656
Your code for the second migration up function should look something like the following:
$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');
Upvotes: 2