Foreign keys

When an RDBMS supports foreign keys, its table class implements the weeDbMetaForeignKeyProvider.

Fetching a table's foreign keys list

A table can have many foreign keys. To retrieve them all, use the table object's foreignKeys method. A foreign key is an instance of weeDbMetaForeignKey.

<?php

$aForeignKeys = $oTable->foreignKeys();

Checking whether a foreign key exists in a table

To check whether a foreign key exists in a table, call the foreignKeyExists method.

<?php

$bForeignKeyExists = $oTable->foreignKeyExists('some_foreign_key');

Fetching a given foreign key from a table

To fetch a given foreign key from a table, please use the foreignKey method of your table object. The returned object is an instance of weeDbMetaForeignKey.

If the foreign key does not exist in the table, an UnexpectedValueException is thrown.

<?php

try {
	$oForeignKey = $oTable->foreignKey('some_foreign_key');
} catch (UnexpectedValueException $e) {
	// the foreign key "some_foreign_key" does not exist in the table.
}

Fetching the names of the columns of a foreign key

To retrieve the names of the columns of a foreign key, take a look to the columnsNames method.

<?php

$aColumnsNames = $oForeignKey->columnsNames();

Retrieving the name of the referenced table

A foreign key references a table in the database. To obtain the name of this table, you can use the referencedTableName method of your foreign key object.

<?php

$sReferencedTableName = $oForeignKey->referencedTableName();

Retrieving the name of the referenced schema

When the underlying database supports both schemas and foreign keys, its foreign key class extends weeDbMetaSchemaForeignKey. To retrieve the name of the schema of the referenced table, use the referencedSchemaName method.

<?php

$sReferencedSchemaName = $oForeignKey->referencedSchemaName();

Retrieving the names of the referenced columns

When you want to know which are the columns from the referenced table referenced to by the foreign key, use the referencedColumnsNames method.

<?php

$aReferencedColumnsNames = $oForeignKey->referencedColumnsNames();