Last active
August 10, 2016 10:17
-
-
Save jamband/4494162 to your computer and use it in GitHub Desktop.
ほんのり改良した DumpSchemaCommand.php
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
<?php | |
class DumpSchemaCommand extends CConsoleCommand | |
{ | |
/** | |
* @see CConsoleCommand::getHelp() | |
*/ | |
public function getHelp() | |
{ | |
return <<<EOD | |
USAGE | |
yiic dumpschema <table_name> | |
EOD; | |
} | |
/** | |
* @see CConsoleCommand::run() | |
*/ | |
public function run($args) | |
{ | |
if (!isset($args[0])) { | |
$this->usageError('テーブル名を入力してください。'); | |
} | |
$schema = $args[0]; | |
$tables = Yii::app()->db->schema->getTables($schema); | |
$result = ''; | |
foreach ($tables as $table) { | |
$result .= "// " . $table->name . "\n"; | |
$result .= "\$this->createTable('" . $table->name . "', array(\n"; | |
foreach ($table->columns as $column) { | |
$result .= " '" . $column->name . "' => '" . $this->getColumnType($column) . "',\n"; | |
} | |
$result .= "), \$options);\n\n"; | |
} | |
echo $result; | |
} | |
/** | |
* Gets the convert column type. | |
* @param string $column the column type | |
* return string the convert column type | |
*/ | |
public function getColumnType($column) | |
{ | |
if ($column->isPrimaryKey) { | |
return 'pk'; | |
} | |
$result = $column->dbType; | |
if (strpos($column->dbType, 'int') !== false) { | |
$result = 'integer'; | |
} | |
if ($column->dbType === 'tinyint(1)') { | |
$result = 'boolean'; | |
} | |
if (strpos($column->dbType, 'varchar') !== false) { | |
$result = 'string'; | |
} | |
if ($column->dbType === 'binary') { | |
$result = 'blob'; | |
} | |
if (!$column->allowNull) { | |
$result .= ' NOT NULL'; | |
} | |
if ($column->defaultValue !== null) { | |
$result .= " DEFAULT \'{$column->defaultValue}\'"; | |
} | |
if ($column->comment !== '') { | |
$result .= " COMMENT \'{$column->comment}\'"; | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment