Skip to content

Instantly share code, notes, and snippets.

@bologer
Created September 6, 2018 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bologer/402b19f120622aad85cc40d89173d1cf to your computer and use it in GitHub Desktop.
Save bologer/402b19f120622aad85cc40d89173d1cf to your computer and use it in GitHub Desktop.
Convert tables to the required collation in Yii2
$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