Last active
October 1, 2023 18:53
-
-
Save mbischof/3049758 to your computer and use it in GitHub Desktop.
How to drop all tables from database using yii
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$tables = Yii::app()->db->schema->getTableNames(); | |
foreach ($tables as $table) { | |
Yii::app()->db->createCommand()->dropTable($table); | |
} |
Hey, don't forget about foreign_key_checks.
<?php
Yii::app()->db->createCommand("SET foreign_key_checks = 0")->execute();
$tables = Yii::app()->db->schema->getTableNames();
foreach ($tables as $table) {
Yii::app()->db->createCommand()->dropTable($table);
}
Yii::app()->db->createCommand("SET foreign_key_checks = 1")->execute();
Works a little better.
Neither one of your code should work. You need to add ->execute()
after dropTable()
.
Also, app()
doesn't exist... It should be $app
.
Fixed code below, or view my Fork of this Gist:
<?php
Yii::$app->db->createCommand("SET foreign_key_checks = 0")->execute();
$tables = Yii::$app->db->schema->getTableNames();
foreach ($tables as $table) {
Yii::$app->db->createCommand()->dropTable($table)->execute();
}
Yii::$app->db->createCommand("SET foreign_key_checks = 1")->execute();
Hope it helps 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to import tables ?