When an RDBMS supports foreign keys, its table class implements the weeDbMetaForeignKeyProvider.
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();
To check whether a foreign key exists in a table, call the foreignKeyExists method.
<?php
$bForeignKeyExists = $oTable->foreignKeyExists('some_foreign_key');
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.
}
To retrieve the names of the columns of a foreign key, take a look to the columnsNames method.
<?php
$aColumnsNames = $oForeignKey->columnsNames();
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();
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();