Commit b1fc13a3 by Luciano Baraglia

GII unique indexes avoid autoIncrement columns

parent bdafb4be
......@@ -240,7 +240,6 @@ class Generator extends \yii\gii\Generator
}
}
}
$rules = [];
foreach ($types as $type => $columns) {
$rules[] = "[['" . implode("', '", $columns) . "'], '$type']";
......@@ -248,12 +247,16 @@ class Generator extends \yii\gii\Generator
foreach ($lengths as $length => $columns) {
$rules[] = "[['" . implode("', '", $columns) . "'], 'string', 'max' => $length]";
}
// Unique indexes rules
try {
$db = $this->getDbConnection();
$uniqueIndexes = $db->getSchema()->findUniqueIndexes($table);
foreach ($uniqueIndexes as $indexName => $uniqueColumns) {
// Avoid validating auto incrementable columns
if (!$this->isUniqueColumnAutoIncrementable($table, $uniqueColumns)) {
$attributesCount = count($uniqueColumns);
if ($attributesCount == 1) {
$rules[] = "[['" . $uniqueColumns[0] . "'], 'unique']";
} elseif ($attributesCount > 1) {
......@@ -263,6 +266,7 @@ class Generator extends \yii\gii\Generator
$rules[] = "[['" . $columnsList . "'], 'unique', 'targetAttribute' => ['" . $columnsList . "'], 'message' => 'The combination of " . implode(', ', $labels) . " and " . $lastLabel . " has already been taken.']";
}
}
}
} catch (NotSupportedException $e) {
// doesn't support unique indexes information...do nothing
}
......@@ -570,4 +574,20 @@ class Generator extends \yii\gii\Generator
{
return Yii::$app->{$this->db};
}
/**
* Checks if any of the specified columns of an unique index is auto incrementable.
* @param \yii\db\TableSchema $table the table schema
* @param array $columns columns to check for autoIncrement property
* @return boolean whether any of the specified columns is auto incrementable.
*/
protected function isUniqueColumnAutoIncrementable($table, $columns)
{
foreach ($columns as $column) {
if ($table->columns[$column]->autoIncrement) {
return true;
}
}
return false;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment