mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-22 11:21:01 +08:00
GII unique indexes avoid autoIncrement columns
This commit is contained in:
@ -240,7 +240,6 @@ class Generator extends \yii\gii\Generator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$rules = [];
|
$rules = [];
|
||||||
foreach ($types as $type => $columns) {
|
foreach ($types as $type => $columns) {
|
||||||
$rules[] = "[['" . implode("', '", $columns) . "'], '$type']";
|
$rules[] = "[['" . implode("', '", $columns) . "'], '$type']";
|
||||||
@ -248,19 +247,24 @@ class Generator extends \yii\gii\Generator
|
|||||||
foreach ($lengths as $length => $columns) {
|
foreach ($lengths as $length => $columns) {
|
||||||
$rules[] = "[['" . implode("', '", $columns) . "'], 'string', 'max' => $length]";
|
$rules[] = "[['" . implode("', '", $columns) . "'], 'string', 'max' => $length]";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unique indexes rules
|
// Unique indexes rules
|
||||||
try {
|
try {
|
||||||
$db = $this->getDbConnection();
|
$db = $this->getDbConnection();
|
||||||
$uniqueIndexes = $db->getSchema()->findUniqueIndexes($table);
|
$uniqueIndexes = $db->getSchema()->findUniqueIndexes($table);
|
||||||
foreach ($uniqueIndexes as $indexName => $uniqueColumns) {
|
foreach ($uniqueIndexes as $indexName => $uniqueColumns) {
|
||||||
$attributesCount = count($uniqueColumns);
|
// Avoid validating auto incrementable columns
|
||||||
if ($attributesCount == 1) {
|
if (!$this->isUniqueColumnAutoIncrementable($table, $uniqueColumns)) {
|
||||||
$rules[] = "[['" . $uniqueColumns[0] . "'], 'unique']";
|
$attributesCount = count($uniqueColumns);
|
||||||
} elseif ($attributesCount > 1) {
|
|
||||||
$labels = array_intersect_key($this->generateLabels($table), array_flip($uniqueColumns));
|
if ($attributesCount == 1) {
|
||||||
$lastLabel = array_pop($labels);
|
$rules[] = "[['" . $uniqueColumns[0] . "'], 'unique']";
|
||||||
$columnsList = implode("', '", $uniqueColumns);
|
} elseif ($attributesCount > 1) {
|
||||||
$rules[] = "[['" . $columnsList . "'], 'unique', 'targetAttribute' => ['" . $columnsList . "'], 'message' => 'The combination of " . implode(', ', $labels) . " and " . $lastLabel . " has already been taken.']";
|
$labels = array_intersect_key($this->generateLabels($table), array_flip($uniqueColumns));
|
||||||
|
$lastLabel = array_pop($labels);
|
||||||
|
$columnsList = implode("', '", $uniqueColumns);
|
||||||
|
$rules[] = "[['" . $columnsList . "'], 'unique', 'targetAttribute' => ['" . $columnsList . "'], 'message' => 'The combination of " . implode(', ', $labels) . " and " . $lastLabel . " has already been taken.']";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (NotSupportedException $e) {
|
} catch (NotSupportedException $e) {
|
||||||
@ -570,4 +574,20 @@ class Generator extends \yii\gii\Generator
|
|||||||
{
|
{
|
||||||
return Yii::$app->{$this->db};
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user