function Column::validate
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Database/SchemaDefinition/Column.php \Drupal\Core\Database\SchemaDefinition\Column::validate()
Validates the properties of the value object.
Throws
\Drupal\Core\Database\Exception\SchemaDefinitionException
File
-
core/
lib/ Drupal/ Core/ Database/ SchemaDefinition/ Column.php, line 132
Class
- Column
- Describes a database table's column.
Namespace
Drupal\Core\Database\SchemaDefinitionCode
private function validate() : void {
// If no column type specified, a db specific one should be set.
if ($this->type === NULL && $this->dbSpecificExtra === NULL) {
throw new SchemaDefinitionException("Neither 'type' nor 'dbSpecificExtra' for column '{$this->name}'");
}
// Cannot set notNull for serial columns.
if ($this->notNull !== NULL && $this->type === ColumnType::Serial) {
throw new SchemaDefinitionException("Cannot set 'notNull' for {$this->type->value} column '{$this->name}'");
}
// Can only set unsigned for some column types.
if ($this->unsigned !== NULL && $this->type !== NULL && !in_array($this->type, [
ColumnType::Int,
ColumnType::Float,
ColumnType::Numeric,
], TRUE)) {
throw new SchemaDefinitionException("Cannot set 'unsigned' for {$this->type->value} column '{$this->name}'");
}
// Can only set length for some column types.
if ($this->length !== NULL && $this->type !== NULL && !in_array($this->type, [
ColumnType::Char,
ColumnType::Varchar,
ColumnType::VarcharAscii,
ColumnType::Text,
], TRUE)) {
throw new SchemaDefinitionException("Cannot set 'length' for {$this->type->value} column '{$this->name}'");
}
// Can only set binary for some column types.
if ($this->binary !== NULL && $this->type !== NULL && !in_array($this->type, [
ColumnType::Char,
ColumnType::Varchar,
ColumnType::VarcharAscii,
ColumnType::Text,
], TRUE)) {
throw new SchemaDefinitionException("Cannot set 'binary' for {$this->type->value} column '{$this->name}'");
}
// Can only set scale of a numeric column.
if ($this->precision !== NULL && $this->type !== NULL && $this->type !== ColumnType::Numeric) {
throw new SchemaDefinitionException("Cannot set 'precision' for {$this->type->value} column '{$this->name}'");
}
// Can only set precision of a numeric column.
if ($this->scale !== NULL && $this->type !== NULL && $this->type !== ColumnType::Numeric) {
throw new SchemaDefinitionException("Cannot set 'scale' for {$this->type->value} column '{$this->name}'");
}
// Can only set serialize on a text/blob column.
if ($this->serialize !== NULL && $this->type !== NULL && !in_array($this->type, [
ColumnType::Text,
ColumnType::Blob,
], TRUE)) {
throw new SchemaDefinitionException("Cannot set 'serialize' for {$this->type->value} column '{$this->name}'");
}
// Check validity of default type.
if ($this->type !== NULL && $this->default !== NULL && !$this->type
->isValidDefaultValue($this->default)) {
throw new SchemaDefinitionException("Cannot set a {$this->default->name} default for column '{$this->name}'");
}
// Cannot set a null value if notNull is true.
if ($this->notNull && $this->default && $this->default->value === NULL) {
throw new SchemaDefinitionException("Cannot set a null default for column '{$this->name}': notNull is true");
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.