public function Schema::addIndex in Drupal 9
Same name in this branch
- 9 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::addIndex()
- 9 core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php \Drupal\Core\Database\Driver\sqlite\Schema::addIndex()
- 9 core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::addIndex()
- 9 core/lib/Drupal/Core/Database/Driver/mysql/Schema.php \Drupal\Core\Database\Driver\mysql\Schema::addIndex()
- 9 core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses/Schema.php \Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Schema::addIndex()
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Database/Driver/mysql/Schema.php \Drupal\Core\Database\Driver\mysql\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.
Overrides Schema::addIndex
See also
File
- core/
lib/ Drupal/ Core/ Database/ Driver/ mysql/ Schema.php, line 562
Class
- Schema
- MySQL implementation of \Drupal\Core\Database\Schema.
Namespace
Drupal\Core\Database\Driver\mysqlCode
public function addIndex($table, $name, $fields, array $spec) {
if (!$this
->tableExists($table)) {
throw new SchemaObjectDoesNotExistException("Cannot add index '{$name}' to table '{$table}': table doesn't exist.");
}
if ($this
->indexExists($table, $name)) {
throw new SchemaObjectExistsException("Cannot add index '{$name}' to table '{$table}': index already exists.");
}
$spec['indexes'][$name] = $fields;
$indexes = $this
->getNormalizedIndexes($spec);
$this->connection
->query('ALTER TABLE {' . $table . '} ADD INDEX `' . $name . '` (' . $this
->createKeySql($indexes[$name]) . ')');
}