Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2迁移事务处理方法,safeup()和safedown()不工作_Php_Mysql_Yii - Fatal编程技术网

Php Yii2迁移事务处理方法,safeup()和safedown()不工作

Php Yii2迁移事务处理方法,safeup()和safedown()不工作,php,mysql,yii,Php,Mysql,Yii,我在yii中使用safeUp和safeDown方法处理数据库事务,但它似乎不起作用。下面是我的代码: public function safeUp() { $this->createTable(self::TABLE_EMPLOYMENT_HISTORY, [ 'id' => $this->integer() . ' UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', 'user_id'

我在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);

}
当我运行它时,它当然会说,在尝试创建外键时,table
table\u mission
不存在。我本来希望回滚,但没有发生,而是继续创建“就业历史”表

为什么尽管使用了safeup方法,它仍要创建表

非常感谢您的帮助。

来自:

某些语句无法回滚。一般来说,这些包括数据 定义语言(DDL)语句,如创建或 删除数据库,即创建、删除或更改表或存储 例行程序

你应该设计你的交易不包括这样的陈述。如果 您在无法滚动的事务的早期发出语句 返回,然后另一个语句随后失败,则 在这种情况下,无法通过发出
ROLLBACK
语句


所以这是正常的行为。例如,PostgreSQL没有这个问题。

数据库可能不支持创建表的事务。据我所知,MySql没有。因此,您的代码触发回滚,但MySql本身无法回滚表创建,因为它不支持此操作。好的,谢谢@chris---Hi@chris---我现在遇到了这个问题。等待你的答复。那又怎样?进一步看:哦,我的糟糕。谢谢你,克里斯---