public function Schema::addIndex

public Schema::addIndex($table, $name, $fields, array $spec)

Add an index.

@todo remove the $spec argument whenever schema introspection is added.

Parameters

$table: The table to be altered.

$name: The name of the index.

$fields: An array of field names or field information; if field information is passed, it's an array whose first element is the field name and whose second is the maximum length in the index. For example, the following will use the full length of the `foo` field, but limit the `bar` field to 4 characters:

    $fields = ['foo', ['bar', 4]];
  

array $spec: The table specification for the table to be altered. This is used in order to be able to ensure that the index length is not too long. This schema definition can usually be obtained through hook_schema(), or in case the table was created by the Entity API, through the schema handler listed in the entity class definition. For reference, see SqlContentEntityStorageSchema::getDedicatedTableSchema() and SqlContentEntityStorageSchema::getSharedTableFieldSchema().

In order to prevent human error, it is recommended to pass in the complete table specification. However, in the edge case of the complete table specification not being available, we can pass in a partial table definition containing only the fields that apply to the index:

  $spec = [
    // Example partial specification for a table:
    'fields' => [
      'example_field' => [
        'description' => 'An example field',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ],
    ],
    'indexes' => [
      'table_example_field' => ['example_field'],
    ],
  ];
  

Note that the above is a partial table definition and that we would usually pass a complete table definition as obtained through hook_schema() instead.

Throws

\Drupal\Core\Database\SchemaObjectDoesNotExistException If the specified table doesn't exist.

\Drupal\Core\Database\SchemaObjectExistsException If the specified table already has an index by that name.

Overrides Schema::addIndex

See also

Schema API

hook_schema()

File

core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php, line 590

Class

Schema
SQLite implementation of \Drupal\Core\Database\Schema.

Namespace

Drupal\Core\Database\Driver\sqlite

Code

public function addIndex($table, $name, $fields, array $spec) {
  if (!$this->tableExists($table)) {
    throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name)));
  }
  if ($this->indexExists($table, $name)) {
    throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", array('@table' => $table, '@name' => $name)));
  }

  $schema['indexes'][$name] = $fields;
  $statements = $this->createIndexSql($table, $schema);
  foreach ($statements as $statement) {
    $this->connection->query($statement);
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Database!Driver!sqlite!Schema.php/function/Schema::addIndex/8.1.x