Created
September 6, 2018 08:44
-
-
Save bologer/402b19f120622aad85cc40d89173d1cf to your computer and use it in GitHub Desktop.
Convert tables to the required collation in Yii2
This file contains hidden or 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
$wantedCollation = 'utf8_unicode_ci'; | |
$command = Yii::$app->getDb()->createCommand('SHOW TABLE STATUS'); | |
$tableCollations = $command->queryAll(); | |
if (!empty($tableCollations)) { | |
foreach ($tableCollations as $tableCollation) { | |
$tableName = $tableCollation['Name'] ?? null; | |
$collation = $tableCollation['Collation'] ?? null; | |
if ($tableName && $collation !== null && trim($collation) !== $wantedCollation) { | |
$sql = "ALTER TABLE $tableName CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;"; | |
$this->execute($sql); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment