createIndex() public method

Builds and executes a SQL statement for creating a new index.
public createIndex ( string $name, string $table, string | array $columns, boolean $unique = false )
$name string the name of the index. The name will be properly quoted by the method.
$table string the table that the new index will be created for. The table name will be properly quoted by the method.
$columns string | array the column(s) that should be included in the index. If there are multiple columns, please separate them by commas or use an array. Each column name will be properly quoted by the method. Quoting will be skipped for column names that include a left parenthesis "(".
$unique boolean whether to add UNIQUE constraint on the created index.
Exemplo n.º 1
0
 public function createIndex($name, $table, $columns, $unique = false)
 {
     if (is_null($name)) {
         $name = self::formIndexName($table, $columns, $unique ? "unq" : "idx");
     }
     return parent::createIndex($name, $table, $columns, $unique);
 }
Exemplo n.º 2
0
 /**
  * Builds and executes a SQL statement for creating a new index.
  * @param string $name the name of the index. The name will be properly quoted by the method.
  * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method.
  * @param string $column the column(s) that should be included in the index. If there are multiple columns, please separate them
  * by commas or use an array. The column names will be properly quoted by the method.
  * @param boolean $unique whether to add UNIQUE constraint on the created index.
  */
 public function createIndex($name, $table, $column, $unique = false)
 {
     if (in_array($this->db->getDriverName(), array('mysql', 'mysqli')) && $table != strtolower($table)) {
         echo "    > create (via alter table)" . ($unique ? ' unique' : '') . " index {$name} on {$table} (" . implode(',', (array) $column) . ") ...";
         $time = microtime(true);
         $sql = 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' ADD ' . ($unique ? 'UNIQUE INDEX' : 'INDEX') . ' ' . $this->db->quoteTableName($name) . ' (' . $this->db->getQueryBuilder()->buildColumns($column) . ')';
         $this->db->createCommand($sql)->execute();
         echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
     } else {
         parent::createIndex($name, $table, $column, $unique);
     }
 }
Exemplo n.º 3
0
 /**
  * @inheritdoc
  * Note: table will be auto pefixied if [[$autoWrapTableNames]] is true.
  */
 public function createIndex($name, $table, $columns, $unique = false)
 {
     $table = $this->autoWrappedTableName($table);
     return parent::createIndex($name, $table, $columns, $unique);
 }
Exemplo n.º 4
0
 /**
  * @inheritdoc
  * @param string|null $name
  */
 public function createIndex($name, $table, $columns, $unique = false)
 {
     if (is_null($name)) {
         $name = implode('-', (array) $columns);
     }
     parent::createIndex($name, $table, $columns, $unique);
 }
Exemplo n.º 5
0
 /**
  * Builds and executes a SQL statement for creating a new index.
  * @param string $name the name of the index. The name will be properly quoted by the method.
  * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method.
  * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, please separate them
  * by commas or use an array. Each column name will be properly quoted by the method. Quoting will be skipped for column names that
  * include a left parenthesis "(".
  * @param boolean $unique whether to add UNIQUE constraint on the created index.
  */
 public function createIndex($table, $columns, $unique = false, $name = null)
 {
     $name = $name ?: $this->getNameIndex($table, $columns);
     parent::createIndex($name, $table, $columns, $unique);
 }