function Table::validate

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Database/SchemaDefinition/Table.php \Drupal\Core\Database\SchemaDefinition\Table::validate()

Validates the properties of the value object.

Throws

\Drupal\Core\Database\Exception\SchemaDefinitionException

1 call to Table::validate()
Table::__construct in core/lib/Drupal/Core/Database/SchemaDefinition/Table.php
Constructor.

File

core/lib/Drupal/Core/Database/SchemaDefinition/Table.php, line 64

Class

Table
Describes a database table.

Namespace

Drupal\Core\Database\SchemaDefinition

Code

private function validate() : void {
  if ($this->columns && !Inspector::assertAllObjects($this->columns, Column::class)) {
    throw new SchemaDefinitionException("All members of the 'columns' argument of the '{$this->name}' Table must be Column objects");
  }
  if ($this->uniqueKeys && !Inspector::assertAllObjects($this->uniqueKeys, UniqueKey::class)) {
    throw new SchemaDefinitionException("All members of the 'uniqueKeys' argument of the '{$this->name}' Table must be UniqueKey objects");
  }
  if ($this->indexes && !Inspector::assertAllObjects($this->indexes, Index::class)) {
    throw new SchemaDefinitionException("All members of the 'indexes' argument of the '{$this->name}' Table must be Index objects");
  }
  if ($this->foreignKeys && !Inspector::assertAllObjects($this->foreignKeys, ForeignKey::class)) {
    throw new SchemaDefinitionException("All members of the 'foreignKeys' argument of the '{$this->name}' Table must be ForeignKey objects");
  }
  // MariaDB and SQLite do not allow generated columns in the primary key.
  if ($this->columns && $this->primaryKey) {
    $generatedColumnNames = array_map(static fn(Column $column): string => $column->name, array_filter($this->columns, static fn(Column $column): bool => $column->generatedExpression !== NULL));
    $primaryKeyColumnNames = array_map(static fn(KeyColumn $keyColumn): string => $keyColumn->name, $this->primaryKey->columns);
    if (array_intersect($primaryKeyColumnNames, $generatedColumnNames)) {
      throw new SchemaDefinitionException("The primary key of the '{$this->name}' Table cannot include generated columns");
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.