public function generateUniqueRules($table)
    {
        $rules = [];
        $uniqueIndexes = $this->getDbConnection()->getSchema()->findUniqueIndexes($table);
        foreach ($uniqueIndexes as $uniqueColumns) {
            // Avoid validating auto incremental columns
            if ($this->isColumnAutoIncremental($table, $uniqueColumns)) {
                continue;
            }
            $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);
                $labels = implode(', ', $labels);
                $columnsList = implode("', '", $uniqueColumns);
                $rules[] = "[
                ['" . $columnsList . "'], 'unique', 'targetAttribute' => ['" . $columnsList . "'],
                'message' => Yii::t('app', 'The combination of {labels} and {lastLabel} has already been taken.', [
                    'labels' => '{$labels}',
                    'lastLabel' => '{$lastLabel}',
                ]),
            ],";
            }
        }

        return $rules;
    }
public function generateUniqueRules($table)
    {
        $rules = [];
        $uniqueIndexes = $this->getDbConnection()->getSchema()->findUniqueIndexes($table);
        foreach ($uniqueIndexes as $uniqueColumns) {
            // Avoid validating auto incremental columns
            if ($this->isColumnAutoIncremental($table, $uniqueColumns)) {
                continue;
            }
            $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);
                $labels = implode(', ', $labels);
                $columnsList = implode("', '", $uniqueColumns);
                $rules[] = "[
                ['" . $columnsList . "'], 'unique', 'targetAttribute' => ['" . $columnsList . "'],
                'message' => Yii::t('app', 'The combination of {labels} and {lastLabel} has already been taken.', [
                    'labels' => '{$labels}',
                    'lastLabel' => '{$lastLabel}',
                ]),
            ],";
            }
        }

        return $rules;
    }
public function generateUniqueRules ( $ table ) { $ rules = [ ] ; $ uniqueIndexes = $ this -> getDbConnection ( ) -> getSchema ( ) -> findUniqueIndexes ( $ table ) ; foreach ( $ uniqueIndexes as $ uniqueColumns ) { // Avoid validating auto incremental columns if ( $ this -> isColumnAutoIncremental ( $ table , $ uniqueColumns ) ) { continue ; } $ 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 ) ; $ labels = implode ( ', ' , $ labels ) ; $ columnsList = implode ( "', '" , $ uniqueColumns ) ; $ rules [ ] = "[
                ['" . $ columnsList . "'], 'unique', 'targetAttribute' => ['" . $ columnsList . "'],
                'message' => Yii::t('app', 'The combination of {labels} and {lastLabel} has already been taken.', [
                    'labels' => '{$labels}',
                    'lastLabel' => '{$lastLabel}',
                ]),
            ]," ; } } return $ rules ; }
Generates validation rules for the unique indexes of specified table.
@param \yii\db\TableSchema $table the table schema
@return array the generated unique validation rules
Generates validation rules for the unique indexes of specified table .