2016-01-03 05:48:37 +05:30
|
|
|
<?php
|
|
|
|
namespace console\db;
|
|
|
|
|
|
|
|
use yii\db\Migration as YiiMigration;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property string $tableOptions
|
|
|
|
*/
|
|
|
|
class Migration extends YiiMigration {
|
|
|
|
|
|
|
|
public function getTableOptions($engine = 'InnoDB') {
|
|
|
|
$tableOptions = null;
|
|
|
|
if ($this->db->driverName === 'mysql') {
|
2017-12-23 03:02:36 +05:30
|
|
|
$tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=' . $engine;
|
2016-01-03 05:48:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $tableOptions;
|
|
|
|
}
|
|
|
|
|
2016-08-21 03:54:23 +05:30
|
|
|
public function createTable($table, $columns, $options = null) {
|
|
|
|
if ($options === null) {
|
|
|
|
$options = $this->getTableOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::createTable($table, $columns, $options);
|
|
|
|
}
|
|
|
|
|
2016-02-14 23:20:10 +05:30
|
|
|
protected function primary(...$columns) {
|
|
|
|
switch (count($columns)) {
|
|
|
|
case 0:
|
|
|
|
$key = '';
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
$key = $columns[0];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$key = $this->buildKey($columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
return " PRIMARY KEY ($key) ";
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildKey(array $columns) {
|
|
|
|
$key = '';
|
|
|
|
foreach ($columns as $i => $column) {
|
2018-04-18 02:17:25 +05:30
|
|
|
$key .= $i === count($columns) ? $column : "$column,";
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
2016-01-03 05:48:37 +05:30
|
|
|
}
|