From bd07bc1dcdc1291e5f11e8ab5f0810768c6a88c9 Mon Sep 17 00:00:00 2001 From: Luciano Baraglia Date: Thu, 26 Dec 2013 02:18:40 -0300 Subject: [PATCH 1/3] GII generates rules for unique indexes --- extensions/yii/gii/generators/model/Generator.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/extensions/yii/gii/generators/model/Generator.php b/extensions/yii/gii/generators/model/Generator.php index 591cb3b..bcc2ea2 100644 --- a/extensions/yii/gii/generators/model/Generator.php +++ b/extensions/yii/gii/generators/model/Generator.php @@ -13,6 +13,7 @@ use yii\db\Connection; use yii\db\Schema; use yii\gii\CodeFile; use yii\helpers\Inflector; +use yii\base\NotSupportedException; /** * This generator will generate one or multiple ActiveRecord classes for the specified database table. @@ -247,7 +248,24 @@ 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) { + $attributesCount = count($uniqueColumns); + if ($attributesCount == 1) { + $rules[] = "['" . $uniqueColumns[0] . "', 'unique']"; + } elseif ($attributesCount > 1) { + $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) { + // doesn't support unique indexes information...do nothing + } return $rules; } From bdafb4becb1df20c459ad3d3fe4476d397b6c018 Mon Sep 17 00:00:00 2001 From: Luciano Baraglia Date: Thu, 26 Dec 2013 02:43:35 -0300 Subject: [PATCH 2/3] Unique indexes rules for single columns into array --- extensions/yii/gii/generators/model/Generator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/yii/gii/generators/model/Generator.php b/extensions/yii/gii/generators/model/Generator.php index bcc2ea2..0a00610 100644 --- a/extensions/yii/gii/generators/model/Generator.php +++ b/extensions/yii/gii/generators/model/Generator.php @@ -255,7 +255,7 @@ class Generator extends \yii\gii\Generator foreach ($uniqueIndexes as $indexName => $uniqueColumns) { $attributesCount = count($uniqueColumns); if ($attributesCount == 1) { - $rules[] = "['" . $uniqueColumns[0] . "', 'unique']"; + $rules[] = "[['" . $uniqueColumns[0] . "'], 'unique']"; } elseif ($attributesCount > 1) { $labels = array_intersect_key($this->generateLabels($table), array_flip($uniqueColumns)); $lastLabel = array_pop($labels); From b1fc13a31ceadc9317d334ca8c37bb11e9224cae Mon Sep 17 00:00:00 2001 From: Luciano Baraglia Date: Thu, 26 Dec 2013 16:42:26 -0300 Subject: [PATCH 3/3] GII unique indexes avoid autoIncrement columns --- extensions/yii/gii/generators/model/Generator.php | 38 +++++++++++++++++------ 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/extensions/yii/gii/generators/model/Generator.php b/extensions/yii/gii/generators/model/Generator.php index 0a00610..785d311 100644 --- a/extensions/yii/gii/generators/model/Generator.php +++ b/extensions/yii/gii/generators/model/Generator.php @@ -240,7 +240,6 @@ class Generator extends \yii\gii\Generator } } } - $rules = []; foreach ($types as $type => $columns) { $rules[] = "[['" . implode("', '", $columns) . "'], '$type']"; @@ -248,19 +247,24 @@ 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) { - $attributesCount = count($uniqueColumns); - if ($attributesCount == 1) { - $rules[] = "[['" . $uniqueColumns[0] . "'], 'unique']"; - } elseif ($attributesCount > 1) { - $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.']"; + // Avoid validating auto incrementable columns + if (!$this->isUniqueColumnAutoIncrementable($table, $uniqueColumns)) { + $attributesCount = count($uniqueColumns); + + if ($attributesCount == 1) { + $rules[] = "[['" . $uniqueColumns[0] . "'], 'unique']"; + } elseif ($attributesCount > 1) { + $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) { @@ -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; + } }