m160406_133247_create_junction_portfolio_and_user.php
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
use yii\db\Migration;
/**
* Handles the creation for table `portfolio_user`.
* Has foreign keys to the tables:
* - `portfolio`
* - `user`
*/
class m160406_133247_create_junction_portfolio_and_user extends Migration
{
/**
* @inheritdoc
*/
public function up()
{
$this->createTable('portfolio_user', [
'portfolio_user_id' => $this->primaryKey(),
'portfolio_id' => $this->integer()
->notNull(),
'user_id' => $this->integer()
->notNull(),
'position' => $this->string(),
'time' => $this->integer(),
'status' => $this->integer()
->defaultValue(2)
->notNull(),
]);
// add foreign key for table `portfolio`
$this->addForeignKey('fk-portfolio_user-portfolio_id', 'portfolio_user', 'portfolio_id', 'portfolio', 'portfolio_id', 'CASCADE');
// add foreign key for table `user`
$this->addForeignKey('fk-portfolio_user-user_id', 'portfolio_user', 'user_id', 'user', 'id', 'CASCADE');
$this->createIndex('unique_portfolio_user_key', 'portfolio_user', ['portfolio_id', 'user_id'], true);
}
/**
* @inheritdoc
*/
public function down()
{
$this->dropIndex('unique_portfolio_user_key', 'portfolio_user');
// drops foreign key for table `portfolio`
$this->dropForeignKey('fk-portfolio_user-portfolio_id', 'portfolio_user');
// drops foreign key for table `user`
$this->dropForeignKey('fk-portfolio_user-user_id', 'portfolio_user');
$this->dropTable('portfolio_user');
}
}