You are here

abstract public function Schema::addIndex in Drupal 8

Same name in this branch
  1. 8 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::addIndex()
  2. 8 core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php \Drupal\Core\Database\Driver\sqlite\Schema::addIndex()
  3. 8 core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::addIndex()
  4. 8 core/lib/Drupal/Core/Database/Driver/mysql/Schema.php \Drupal\Core\Database\Driver\mysql\Schema::addIndex()
Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::addIndex()

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.

See also

Schema API

hook_schema()

3 methods override Schema::addIndex()
Schema::addIndex in core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php
Add an index.
Schema::addIndex in core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
Add an index.
Schema::addIndex in core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
Add an index.

File

core/lib/Drupal/Core/Database/Schema.php, line 533

Class

Schema
Provides a base implementation for Database Schema.

Namespace

Drupal\Core\Database

Code

public abstract function addIndex($table, $name, $fields, array $spec);