我在 yii 中使用 safeUp 和 safeDown 方法进行数据库事务,但它似乎不起作用。下面是我的代码:
public function safeUp()
{
$this->createTable(self::TABLE_EMPLOYMENT_HISTORY, [
'id' => $this->integer() . ' UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
'user_id' => $this->integer()->unsigned()->notNull(),
'start_date' => $this->dateTime()->notNull(),
'end_date' => $this->dateTime(),
'role' => $this->string(150)->notNull(),
'position' => $this->string(50)->notNull(),
'achievements' => $this->text(),
'created_at' => $this->dateTime()->notNull(),
'updated_at' => $this->dateTime()->notNull(),
'status' => $this->string(50)->notNull()
]);
$this->addForeignKey('fk_' . self::TABLE_EMPLOYMENT_HISTORY . '_' . self::TABLE_USERS . '_id', self::TABLE_EMPLOYMENT_HISTORY, 'user_id', self::TABLE_USERS, 'id');
$this->addForeignKey('fk_' . self::TABLE_EMPLOYMENT_HISTORY . '_' . self::TABLE_STATUSES . '_key', self::TABLE_MISTAKE, 'status', self::TABLE_STATUSES, 'key');
}
public function safeDown()
{
$this->dropForeignKey('fk_' . self::TABLE_EMPLOYMENT_HISTORY . '_' . self::TABLE_STATUSES . '_key', self::TABLE_EMPLOYMENT_HISTORY);
$this->dropForeignKey('fk_' . self::TABLE_EMPLOYMENT_HISTORY . '_' . self::TABLE_USERS . '_id', self::TABLE_EMPLOYMENT_HISTORY);
$this->dropTable(self::TABLE_EMPLOYMENT_HISTORY);
}
Run Code Online (Sandbox Code Playgroud)
当我运行该命令时,它当然说TABLE_MISTAKE尝试创建外键时该表不存在。我期待回滚,但这并没有发生,相反,它继续创建表“EMPLOYMENT_HISTORY”。
尽管使用了 safeup 方法,为什么还要创建表?
我们将不胜感激您的帮助。
| 归档时间: |
|
| 查看次数: |
2912 次 |
| 最近记录: |