Skip to content

Commit

Permalink
fixed DEFAULT value
Browse files Browse the repository at this point in the history
  • Loading branch information
c006 committed Apr 5, 2017
1 parent 20112a2 commit c4f83d4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion assets/AppUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AppUtility

private $Nw = "\n";

private $array = [];
public $array = [];

private $dbType = '';

Expand Down
30 changes: 18 additions & 12 deletions controllers/DefaultController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace c006\utility\migration\controllers;

use c006\utility\migration\assets\AppAssets;
Expand Down Expand Up @@ -91,7 +92,14 @@ public function actionIndex()
$k = 0;
foreach ($columns->columns as $column) {
$appUtility = new AppUtility($column, $dbType);
$output->addStr($appUtility->string . "',");

$str = "'" . $appUtility->array['name'] . "' => '";
$str .= strtoupper($appUtility->array['dbType']);
$str .= ($appUtility->array['allowNull']) ? ' NULL ' : ' NOT NULL ';
$str .= (strlen($appUtility->array['defaultValue'])) ? " DEFAULT \\'" . (string) $appUtility->array['defaultValue'] . "\\'" : ' ';
$str .= "',";

$output->addStr($str);
if ($column->isPrimaryKey) {
$array['pk'][] = $column->name;
}
Expand Down Expand Up @@ -161,7 +169,6 @@ public function actionIndex()
$out .= "'" . $column->name . "'=>'" . addslashes($row[ $column->name ]) . "',";
}
$out = rtrim($out, ',') . ']);';
// $output->addStr($out);
$array['inserts'][] = $out;
}
}
Expand Down Expand Up @@ -225,11 +232,18 @@ public function actionIndex()
'model' => $model,
'output' => $output->output(),
'output_drop' => $output_drop->output(),
'tables' => self::getTables()
'tables' => self::getTables(),
]
);
}

/**
* @return \string[]
*/
public function getTables()
{
return \Yii::$app->db->getSchema()->getTableNames('', TRUE);
}

protected function mysql_direct($table)
{
Expand Down Expand Up @@ -265,14 +279,6 @@ protected function mysql_direct($table)

}

/**
* @return \string[]
*/
public function getTables()
{
return \Yii::$app->db->getSchema()->getTableNames('', TRUE);
}

}

/**
Expand All @@ -298,7 +304,7 @@ class OutputString extends Object
/**
* @var string
*/
public $outputStringArray = array();
public $outputStringArray = [];

/**
* @var int
Expand Down
23 changes: 12 additions & 11 deletions migrations/m000000_000000_c006_testing.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ public function up()
if (!in_array(Yii::$app->db->tablePrefix.'new_table_2', $tables)) {
if ($dbType == "mysql") {
$this->createTable('{{%new_table_2}}', [
'id' => 'INT(10) UNSIGNED NOT NULL AUTO_INCREMENT',
'column_varchar' => 'VARCHAR(200) NULL',
'column_char' => 'CHAR(3) NULL',
'column_tinyint' => 'TINYINT(1) NULL',
4 => 'PRIMARY KEY (`id`)',
'id' => 'INT(10) UNSIGNED NOT NULL ',
'column_varchar' => 'VARCHAR(200) NULL ',
'column_char' => 'CHAR(3) NULL ',
'column_tinyint' => 'TINYINT(1) NOT NULL DEFAULT \'0\'',
'column_smallint' => 'SMALLINT(4) NULL DEFAULT \'1\'',
5 => 'PRIMARY KEY (`id`)',
], $tableOptions_mysql);
}
}
Expand All @@ -46,19 +47,19 @@ public function up()
if (!in_array(Yii::$app->db->tablePrefix.'new_table', $tables)) {
if ($dbType == "mysql") {
$this->createTable('{{%new_table}}', [
'id' => 'INT(11) NOT NULL AUTO_INCREMENT',
'id2' => 'INT(11) NOT NULL',
'column1' => 'VARCHAR(100) NULL',
'column2' => 'CHAR(3) NOT NULL',
'id' => 'INT(11) NOT NULL ',
'id2' => 'INT(11) NOT NULL ',
'column1' => 'VARCHAR(100) NULL ',
'column2' => 'CHAR(3) NOT NULL ',
4 => 'PRIMARY KEY (`id`,`id2`)',
], $tableOptions_mysql);
}
}


$this->execute('SET foreign_key_checks = 0');
$this->insert('{{%new_table_2}}',['id'=>'1','column_varchar'=>'A','column_char'=>'B','column_tinyint'=>'1']);
$this->insert('{{%new_table_2}}',['id'=>'2','column_varchar'=>'AA','column_char'=>'BB','column_tinyint'=>'0']);
$this->insert('{{%new_table_2}}',['id'=>'1','column_varchar'=>'A','column_char'=>'B','column_tinyint'=>'1','column_smallint'=>'1']);
$this->insert('{{%new_table_2}}',['id'=>'2','column_varchar'=>'AA','column_char'=>'BB','column_tinyint'=>'0','column_smallint'=>'1']);
$this->insert('{{%new_table}}',['id'=>'1','id2'=>'0','column1'=>'A','column2'=>'bbb']);
$this->insert('{{%new_table}}',['id'=>'2','id2'=>'1','column1'=>'b','column2'=>'ccc']);
$this->execute('SET foreign_key_checks = 1;');
Expand Down

0 comments on commit c4f83d4

Please sign in to comment.