function Schema::addIndex
Same name in this branch
- 11.x core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::addIndex()
- 11.x core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\mysql\Schema::addIndex()
- 11.x core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::addIndex()
Same name in other branches
- 9 core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::addIndex()
- 9 core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\mysql\Schema::addIndex()
- 9 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::addIndex()
- 9 core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses/Schema.php \Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Schema::addIndex()
- 9 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::addIndex()
- 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php \Drupal\Core\Database\Driver\sqlite\Schema::addIndex()
- 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Schema.php \Drupal\Core\Database\Driver\mysql\Schema::addIndex()
- 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::addIndex()
- 8.9.x core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::addIndex()
- 10 core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::addIndex()
- 10 core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\mysql\Schema::addIndex()
- 10 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::addIndex()
- 10 core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Schema.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\Schema::addIndex()
- 10 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
4 methods override Schema::addIndex()
- Schema::addIndex in core/
modules/ sqlite/ src/ Driver/ Database/ sqlite/ Schema.php - Add an index.
- Schema::addIndex in core/
modules/ mysql/ src/ Driver/ Database/ mysql/ Schema.php - Add an index.
- Schema::addIndex in core/
modules/ pgsql/ src/ Driver/ Database/ pgsql/ Schema.php - Add an index.
- StubSchema::addIndex in core/
tests/ Drupal/ Tests/ Core/ Database/ Stub/ StubSchema.php - Add an index.
File
-
core/
lib/ Drupal/ Core/ Database/ Schema.php, line 494
Class
- Schema
- Provides a base implementation for Database Schema.
Namespace
Drupal\Core\DatabaseCode
public abstract function addIndex($table, $name, $fields, array $spec);
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.