class ForeignKey
Describes a database foreign key.
Hierarchy
- class \Drupal\Core\Database\SchemaDefinition\KeyBase implements \Drupal\Core\Database\SchemaDefinition\SchemaDefinitionInterface
- class \Drupal\Core\Database\SchemaDefinition\ForeignKey extends \Drupal\Core\Database\SchemaDefinition\KeyBase
Expanded class hierarchy of ForeignKey
4 files declare their use of ForeignKey
- database.api.php in core/
lib/ Drupal/ Core/ Database/ database.api.php - Hooks related to the Database system and the Schema API.
- locale.install in core/
modules/ locale/ locale.install - Install, update, and uninstall functions for the Locale module.
- SchemaDefinitionConversionTest.php in core/
tests/ Drupal/ Tests/ Core/ Database/ SchemaDefinitionConversionTest.php - SchemaDefinitionTest.php in core/
tests/ Drupal/ Tests/ Core/ Database/ SchemaDefinitionTest.php
File
-
core/
lib/ Drupal/ Core/ Database/ SchemaDefinition/ ForeignKey.php, line 12
Namespace
Drupal\Core\Database\SchemaDefinitionView source
final class ForeignKey extends KeyBase {
/**
* The foreign key columns.
*
* @var list<KeyColumn>
* The list of KeyColumn objects of the foreign table.
*/
public readonly array $foreignColumns;
/**
* Constructor.
*
* @param string $name
* The foreign key name.
* @param string $foreignTable
* The foreign (referenced) table name.
* @param list<KeyColumn|string> $columns
* A mix of key column specifiers, being KeyColumn objects, strings naming
* columns, or arrays of two elements, column name and length, specifying a
* prefix of the named column.
* @param list<KeyColumn|string> $foreignColumns
* A mix of key column specifiers of the foreign (referenced) table, being
* KeyColumn objects, strings naming columns, or arrays of two elements,
* column name and length, specifying a prefix of the named column.
*/
public function __construct(public readonly string $name, public readonly string $foreignTable, array $columns, array $foreignColumns) {
parent::__construct($columns, FALSE);
$this->foreignColumns = $this->buildKeyColumns($foreignColumns, FALSE);
$this->validate();
}
/**
* Validates the properties of the value object.
*
* @throws \Drupal\Core\Database\Exception\SchemaDefinitionException
*/
private function validate() : void {
if (count($this->columns) !== count($this->foreignColumns)) {
throw new SchemaDefinitionException("Mismatching count of columns for the '{$this->name}' foreign key");
}
}
/**
* {@inheritdoc}
*/
public function toArray() : array {
$match = [];
for ($i = 0; $i < count($this->columns); $i++) {
$localColumnName = $this->columns[$i]->name;
$foreignColumnName = $this->foreignColumns[$i]->name;
$match[$localColumnName] = $foreignColumnName;
}
return [
'table' => $this->foreignTable,
'columns' => $match,
];
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|
| ForeignKey::$foreignColumns | public | property | The foreign key columns. | |
| ForeignKey::toArray | public | function | Converts the object to legacy array-based specifications. | Overrides KeyBase::toArray |
| ForeignKey::validate | private | function | Validates the properties of the value object. | |
| ForeignKey::__construct | public | function | Constructor. | Overrides KeyBase::__construct |
| KeyBase::$columns | public | property | The key columns. | |
| KeyBase::buildKeyColumns | protected | function | Builds an array of KeyColumn objects from a mixed list of columns. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.